You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by martin waller <ma...@flomerics.co.uk> on 2007/04/18 13:35:19 UTC

Newbie - how to validate xml file?

Hi,

I'm trying to work out how to validate xml files with a simple schema 
I've written and I'm clearly missing something.

Here's the code that I'm using to do the validation:

       //initialisation stuff...

        XercesDOMParser parser;
        parser.setValidationScheme(XercesDOMParser::Val_Always);
        parser.setDoNamespaces(true);
        parser.setDoSchema(true);
        
parser.setExternalNoNamespaceSchemaLocation(testschema.c_str());//the 
schema..
        try
        {
            parser.parse(testfile.c_str());       
        }
        catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);           
        }
        catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);           
        }
        catch (...) {
            cout << "Unexpected Exception \n" ;           
        }

        try
        {
            parser.parse(testfile2.c_str());       
        }
        catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);           
        }
        catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);           
        }
        catch (...) {
            cout << "Unexpected Exception \n" ;           
        }

'testfile' should be valid, and testfile2 isn't, but when I run the 
program 'nothing happens' (it runs, no exceptions, no complaints).

Here's the xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="componentDefinition">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="refDes" type="xsd:string" minOccurs="0" 
maxOccurs="1"/>
            <xsd:element name="packageName" type="xsd:string" 
maxOccurs="1"/>
            <xsd:element name="partNumber" type="xsd:string" 
minOccurs="0" maxOccurs="1"/>
            <xsd:element name="power" type="xsd:float" maxOccurs="1"/>
            <xsd:element name="powerUnits" type="xsd:string" 
minOccurs="0" maxOccurs="1"/>
            <xsd:element name="zRot">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="0"/>
                        <xsd:enumeration value="90"/>
                        <xsd:enumeration value="180"/>
                        <xsd:enumeration value="270"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="length" type="xsd:float" maxOccurs="1"/>
            <xsd:element name="width" type="xsd:float" maxOccurs="1"/>
            <xsd:element name="lengthUnits" type="xsd:string" 
minOccurs="0" maxOccurs="1"/>
            <xsd:element name="height" type="xsd:float" maxOccurs="1"/>
            <xsd:element name="heightUnits" type="xsd:string" 
minOccurs="0" maxOccurs="1"/>
        </xsd:all>                       
        <xsd:element name="property" minOccurs="0" maxOccurs="unbounded">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="propName" type="xsd:string"/>
                    <xsd:element name="propValue" type="xsd:string"/>
                    <xsd:element name="propUnits" type="xsd:string" 
minOccurs="0"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>       
    </xsd:complexType>
</xsd:element>
</xsd:schema>

Here's the invalid xml file testfile2):
<?xml version="1.0" encoding="utf-8" ?>
<componentDefinition xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="CompDef.xsd">
<refDes>U1</refDes>
<rubbish>Test01-02-03</rubbish>
<power>3</power>
<powerUnits>W</powerUnits>
<zRot>0</zRot>
<length>100</length>
<width>50</width>
<lengthUnits>mm</lengthUnits>
<height>4</height>
<heightUnits>mm</heightUnits>
</componentDefinition>



I'm not quite sure what to expect when it parses the invalid file? It 
seems to do it  OK then just returns normally. Shuld it throw an 
exception or what? I'm a bit overwhelmed by the docs...like I say, I'm 
new to this so please treat me as a beginner...

Martin


Xerces support for creating the xml instances from schema document

Posted by Umesh Chandak <um...@gs-lab.com>.
Hi,
     Right now there is no support for creating the xml instance from
the XML schema document. correct me if I am wrong here.
Is there any project going on to add this feature in xerces? What is
it's usability if some body does it? Is there any point in doing this as
a academic project? means does it have any commercial value?

Thanks.
Regards,
Umesh


Re: Newbie - how to validate xml file?

Posted by martin waller <ma...@flomerics.co.uk>.
Thankyou both for your quick responses.

I have altered the code appropriately and now my custom error handler 
(plagiarised from samples/DOMPrint/DOMTreeErrorReporter) correctly 
identifies the invalid xml files. Altered code below so anyone searching 
the archives for a quick simple solution can find it:

//initialisation stuff

    XercesDOMParser parser;
        parser.setValidationScheme(XercesDOMParser::Val_Always);
        parser.setDoNamespaces(true);
        parser.setDoSchema(true);       
        ErrorReporter *errReporter = new ErrorReporter();//see 
/samples/DOMPrint/DOMTreeErrorReporter - just used functionality 
provided in that

        // The object parser calls when it detects violations of the schema.
        parser.setErrorHandler(errReporter);
       
       //schema file - location is in testschema string
        parser.loadGrammar(testschema.c_str(), 
Grammar::SchemaGrammarType, true);

        try//load file with surplus info - should load OK
        {
            parser.parse(testfile.c_str());       
        }
        catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);           
        }
        catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);           
        }
        catch (...) {
            cout << "Unexpected Exception \n" ;           
        }

    //other tests for invalid files - errorhandler prints errors to 
stdout, no exceptions caught in try/catch block as above


Alberto Massari wrote:
> Hi Martin,
> in order to get the validation errors you need to set the error 
> handler; otherwise you can only ask parser.getErrorCount() to detect 
> whether non-fatal errors occurred.
>
> Alberto
>
> At 12.35 18/04/2007 +0100, martin waller wrote:
>> Hi,
>>
>> I'm trying to work out how to validate xml files with a simple schema 
>> I've written and I'm clearly missing something.
>>
>> Here's the code that I'm using to do the validation:
>>
>>       //initialisation stuff...
>>
>>        XercesDOMParser parser;
>>        parser.setValidationScheme(XercesDOMParser::Val_Always);
>>        parser.setDoNamespaces(true);
>>        parser.setDoSchema(true);
>>
>> parser.setExternalNoNamespaceSchemaLocation(testschema.c_str());//the 
>> schema..
>>        try
>>        {
>>            parser.parse(testfile.c_str());
>>        }
>>        catch (const XMLException& toCatch) {
>>            char* message = XMLString::transcode(toCatch.getMessage());
>>            cout << "Exception message is: \n"
>>                 << message << "\n";
>>            XMLString::release(&message);
>>        }
>>        catch (const DOMException& toCatch) {
>>            char* message = XMLString::transcode(toCatch.msg);
>>            cout << "Exception message is: \n"
>>                 << message << "\n";
>>            XMLString::release(&message);
>>        }
>>        catch (...) {
>>            cout << "Unexpected Exception \n" ;
>>        }
>>
>>        try
>>        {
>>            parser.parse(testfile2.c_str());
>>        }
>>        catch (const XMLException& toCatch) {
>>            char* message = XMLString::transcode(toCatch.getMessage());
>>            cout << "Exception message is: \n"
>>                 << message << "\n";
>>            XMLString::release(&message);
>>        }
>>        catch (const DOMException& toCatch) {
>>            char* message = XMLString::transcode(toCatch.msg);
>>            cout << "Exception message is: \n"
>>                 << message << "\n";
>>            XMLString::release(&message);
>>        }
>>        catch (...) {
>>            cout << "Unexpected Exception \n" ;
>>        }
>>
>> 'testfile' should be valid, and testfile2 isn't, but when I run the 
>> program 'nothing happens' (it runs, no exceptions, no complaints).
>>
>> Here's the xsd file:
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>> <xsd:element name="componentDefinition">
>>    <xsd:complexType>
>>        <xsd:all>
>>            <xsd:element name="refDes" type="xsd:string" minOccurs="0" 
>> maxOccurs="1"/>
>>            <xsd:element name="packageName" type="xsd:string" 
>> maxOccurs="1"/>
>>            <xsd:element name="partNumber" type="xsd:string" 
>> minOccurs="0" maxOccurs="1"/>
>>            <xsd:element name="power" type="xsd:float" maxOccurs="1"/>
>>            <xsd:element name="powerUnits" type="xsd:string" 
>> minOccurs="0" maxOccurs="1"/>
>>            <xsd:element name="zRot">
>>                <xsd:simpleType>
>>                    <xsd:restriction base="xsd:string">
>>                        <xsd:enumeration value="0"/>
>>                        <xsd:enumeration value="90"/>
>>                        <xsd:enumeration value="180"/>
>>                        <xsd:enumeration value="270"/>
>>                    </xsd:restriction>
>>                </xsd:simpleType>
>>            </xsd:element>
>>            <xsd:element name="length" type="xsd:float" maxOccurs="1"/>
>>            <xsd:element name="width" type="xsd:float" maxOccurs="1"/>
>>            <xsd:element name="lengthUnits" type="xsd:string" 
>> minOccurs="0" maxOccurs="1"/>
>>            <xsd:element name="height" type="xsd:float" maxOccurs="1"/>
>>            <xsd:element name="heightUnits" type="xsd:string" 
>> minOccurs="0" maxOccurs="1"/>
>>        </xsd:all>
>>        <xsd:element name="property" minOccurs="0" maxOccurs="unbounded">
>>            <xsd:complexType>
>>                <xsd:sequence>
>>                    <xsd:element name="propName" type="xsd:string"/>
>>                    <xsd:element name="propValue" type="xsd:string"/>
>>                    <xsd:element name="propUnits" type="xsd:string" 
>> minOccurs="0"/>
>>                </xsd:sequence>
>>            </xsd:complexType>
>>        </xsd:element>
>>    </xsd:complexType>
>> </xsd:element>
>> </xsd:schema>
>>
>> Here's the invalid xml file testfile2):
>> <?xml version="1.0" encoding="utf-8" ?>
>> <componentDefinition xmlns:xsi = 
>> "http://www.w3.org/2001/XMLSchema-instance"
>> xsi:noNamespaceSchemaLocation="CompDef.xsd">
>> <refDes>U1</refDes>
>> <rubbish>Test01-02-03</rubbish>
>> <power>3</power>
>> <powerUnits>W</powerUnits>
>> <zRot>0</zRot>
>> <length>100</length>
>> <width>50</width>
>> <lengthUnits>mm</lengthUnits>
>> <height>4</height>
>> <heightUnits>mm</heightUnits>
>> </componentDefinition>
>>
>>
>>
>> I'm not quite sure what to expect when it parses the invalid file? It 
>> seems to do it  OK then just returns normally. Shuld it throw an 
>> exception or what? I'm a bit overwhelmed by the docs...like I say, 
>> I'm new to this so please treat me as a beginner...
>>
>> Martin
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email 
> ______________________________________________________________________


Re: Newbie - how to validate xml file?

Posted by Alberto Massari <am...@datadirect.com>.
Hi Martin,
in order to get the validation errors you need to set the error 
handler; otherwise you can only ask parser.getErrorCount() to detect 
whether non-fatal errors occurred.

Alberto

At 12.35 18/04/2007 +0100, martin waller wrote:
>Hi,
>
>I'm trying to work out how to validate xml files with a simple 
>schema I've written and I'm clearly missing something.
>
>Here's the code that I'm using to do the validation:
>
>       //initialisation stuff...
>
>        XercesDOMParser parser;
>        parser.setValidationScheme(XercesDOMParser::Val_Always);
>        parser.setDoNamespaces(true);
>        parser.setDoSchema(true);
>
>parser.setExternalNoNamespaceSchemaLocation(testschema.c_str());//the schema..
>        try
>        {
>            parser.parse(testfile.c_str());
>        }
>        catch (const XMLException& toCatch) {
>            char* message = XMLString::transcode(toCatch.getMessage());
>            cout << "Exception message is: \n"
>                 << message << "\n";
>            XMLString::release(&message);
>        }
>        catch (const DOMException& toCatch) {
>            char* message = XMLString::transcode(toCatch.msg);
>            cout << "Exception message is: \n"
>                 << message << "\n";
>            XMLString::release(&message);
>        }
>        catch (...) {
>            cout << "Unexpected Exception \n" ;
>        }
>
>        try
>        {
>            parser.parse(testfile2.c_str());
>        }
>        catch (const XMLException& toCatch) {
>            char* message = XMLString::transcode(toCatch.getMessage());
>            cout << "Exception message is: \n"
>                 << message << "\n";
>            XMLString::release(&message);
>        }
>        catch (const DOMException& toCatch) {
>            char* message = XMLString::transcode(toCatch.msg);
>            cout << "Exception message is: \n"
>                 << message << "\n";
>            XMLString::release(&message);
>        }
>        catch (...) {
>            cout << "Unexpected Exception \n" ;
>        }
>
>'testfile' should be valid, and testfile2 isn't, but when I run the 
>program 'nothing happens' (it runs, no exceptions, no complaints).
>
>Here's the xsd file:
><?xml version="1.0" encoding="UTF-8"?>
><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
><xsd:element name="componentDefinition">
>    <xsd:complexType>
>        <xsd:all>
>            <xsd:element name="refDes" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>            <xsd:element name="packageName" type="xsd:string" maxOccurs="1"/>
>            <xsd:element name="partNumber" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>            <xsd:element name="power" type="xsd:float" maxOccurs="1"/>
>            <xsd:element name="powerUnits" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>            <xsd:element name="zRot">
>                <xsd:simpleType>
>                    <xsd:restriction base="xsd:string">
>                        <xsd:enumeration value="0"/>
>                        <xsd:enumeration value="90"/>
>                        <xsd:enumeration value="180"/>
>                        <xsd:enumeration value="270"/>
>                    </xsd:restriction>
>                </xsd:simpleType>
>            </xsd:element>
>            <xsd:element name="length" type="xsd:float" maxOccurs="1"/>
>            <xsd:element name="width" type="xsd:float" maxOccurs="1"/>
>            <xsd:element name="lengthUnits" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>            <xsd:element name="height" type="xsd:float" maxOccurs="1"/>
>            <xsd:element name="heightUnits" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>        </xsd:all>
>        <xsd:element name="property" minOccurs="0" maxOccurs="unbounded">
>            <xsd:complexType>
>                <xsd:sequence>
>                    <xsd:element name="propName" type="xsd:string"/>
>                    <xsd:element name="propValue" type="xsd:string"/>
>                    <xsd:element name="propUnits" type="xsd:string" 
> minOccurs="0"/>
>                </xsd:sequence>
>            </xsd:complexType>
>        </xsd:element>
>    </xsd:complexType>
></xsd:element>
></xsd:schema>
>
>Here's the invalid xml file testfile2):
><?xml version="1.0" encoding="utf-8" ?>
><componentDefinition xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
>xsi:noNamespaceSchemaLocation="CompDef.xsd">
><refDes>U1</refDes>
><rubbish>Test01-02-03</rubbish>
><power>3</power>
><powerUnits>W</powerUnits>
><zRot>0</zRot>
><length>100</length>
><width>50</width>
><lengthUnits>mm</lengthUnits>
><height>4</height>
><heightUnits>mm</heightUnits>
></componentDefinition>
>
>
>
>I'm not quite sure what to expect when it parses the invalid file? 
>It seems to do it  OK then just returns normally. Shuld it throw an 
>exception or what? I'm a bit overwhelmed by the docs...like I say, 
>I'm new to this so please treat me as a beginner...
>
>Martin


Re: Newbie - how to validate xml file?

Posted by Umesh Chandak <um...@gs-lab.com>.
Hi ,
 Use the loadGrammar function of the XercesDOMParser class.
setExternalNoNamespaceSchemaLocation is not meant for validating the xml
instance. check it's documentation at
http://xml.apache.org/xerces-c/apiDocs/classAbstractDOMParser.html#z871_13

Thanks.
Regards,
Umesh
On Wed, 2007-04-18 at 12:35 +0100, martin waller wrote:
> Hi,
> 
> I'm trying to work out how to validate xml files with a simple schema 
> I've written and I'm clearly missing something.
> 
> Here's the code that I'm using to do the validation:
> 
>        //initialisation stuff...
> 
>         XercesDOMParser parser;
>         parser.setValidationScheme(XercesDOMParser::Val_Always);
>         parser.setDoNamespaces(true);
>         parser.setDoSchema(true);
>         
> parser.setExternalNoNamespaceSchemaLocation(testschema.c_str());//the 
> schema..
>         try
>         {
>             parser.parse(testfile.c_str());       
>         }
>         catch (const XMLException& toCatch) {
>             char* message = XMLString::transcode(toCatch.getMessage());
>             cout << "Exception message is: \n"
>                  << message << "\n";
>             XMLString::release(&message);           
>         }
>         catch (const DOMException& toCatch) {
>             char* message = XMLString::transcode(toCatch.msg);
>             cout << "Exception message is: \n"
>                  << message << "\n";
>             XMLString::release(&message);           
>         }
>         catch (...) {
>             cout << "Unexpected Exception \n" ;           
>         }
> 
>         try
>         {
>             parser.parse(testfile2.c_str());       
>         }
>         catch (const XMLException& toCatch) {
>             char* message = XMLString::transcode(toCatch.getMessage());
>             cout << "Exception message is: \n"
>                  << message << "\n";
>             XMLString::release(&message);           
>         }
>         catch (const DOMException& toCatch) {
>             char* message = XMLString::transcode(toCatch.msg);
>             cout << "Exception message is: \n"
>                  << message << "\n";
>             XMLString::release(&message);           
>         }
>         catch (...) {
>             cout << "Unexpected Exception \n" ;           
>         }
> 
> 'testfile' should be valid, and testfile2 isn't, but when I run the 
> program 'nothing happens' (it runs, no exceptions, no complaints).
> 
> Here's the xsd file:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <xsd:element name="componentDefinition">
>     <xsd:complexType>
>         <xsd:all>
>             <xsd:element name="refDes" type="xsd:string" minOccurs="0" 
> maxOccurs="1"/>
>             <xsd:element name="packageName" type="xsd:string" 
> maxOccurs="1"/>
>             <xsd:element name="partNumber" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>             <xsd:element name="power" type="xsd:float" maxOccurs="1"/>
>             <xsd:element name="powerUnits" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>             <xsd:element name="zRot">
>                 <xsd:simpleType>
>                     <xsd:restriction base="xsd:string">
>                         <xsd:enumeration value="0"/>
>                         <xsd:enumeration value="90"/>
>                         <xsd:enumeration value="180"/>
>                         <xsd:enumeration value="270"/>
>                     </xsd:restriction>
>                 </xsd:simpleType>
>             </xsd:element>
>             <xsd:element name="length" type="xsd:float" maxOccurs="1"/>
>             <xsd:element name="width" type="xsd:float" maxOccurs="1"/>
>             <xsd:element name="lengthUnits" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>             <xsd:element name="height" type="xsd:float" maxOccurs="1"/>
>             <xsd:element name="heightUnits" type="xsd:string" 
> minOccurs="0" maxOccurs="1"/>
>         </xsd:all>                       
>         <xsd:element name="property" minOccurs="0" maxOccurs="unbounded">
>             <xsd:complexType>
>                 <xsd:sequence>
>                     <xsd:element name="propName" type="xsd:string"/>
>                     <xsd:element name="propValue" type="xsd:string"/>
>                     <xsd:element name="propUnits" type="xsd:string" 
> minOccurs="0"/>
>                 </xsd:sequence>
>             </xsd:complexType>
>         </xsd:element>       
>     </xsd:complexType>
> </xsd:element>
> </xsd:schema>
> 
> Here's the invalid xml file testfile2):
> <?xml version="1.0" encoding="utf-8" ?>
> <componentDefinition xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="CompDef.xsd">
> <refDes>U1</refDes>
> <rubbish>Test01-02-03</rubbish>
> <power>3</power>
> <powerUnits>W</powerUnits>
> <zRot>0</zRot>
> <length>100</length>
> <width>50</width>
> <lengthUnits>mm</lengthUnits>
> <height>4</height>
> <heightUnits>mm</heightUnits>
> </componentDefinition>
> 
> 
> 
> I'm not quite sure what to expect when it parses the invalid file? It 
> seems to do it  OK then just returns normally. Shuld it throw an 
> exception or what? I'm a bit overwhelmed by the docs...like I say, I'm 
> new to this so please treat me as a beginner...
> 
> Martin
> 
>