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 to...@sbc.su.se on 2010/09/15 15:51:20 UTC

force validation against schema (.xsd)

Hi xerces users.
I have a (hopefully) easy question for you experts out there.
I would like to validate my xml file against an xml schema.
The parser complains if my xml is ill-formed but does not validate against
the schema I embedded into the xml. I tried to provide an external
location explicitly but it makes no difference.
(this should be pretty standard, but I can't figure it out...)

I use the following code:

    XMLPlatformUtils::Initialize();
    XercesDOMParser* parser = new XercesDOMParser();
    parser->setValidationScheme(XercesDOMParser::Val_Always);
    parser->setDoSchema(true);
    parser->setDoNamespaces(true);
    parser->setExternalSchemaLocation(schema.c_str());
    parser->loadGrammar(schema.c_str(), Grammar::SchemaGrammarType, true);
    parser->setSkipDTDValidation(false);
    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
    parser->setErrorHandler(errHandler);
    const char* xmlFile = xmloutFN.c_str();
    try{
      parser->parse(xmloutFN.c_str());
    }

where:
"schema" is a string in the form "myNamespace /home/myHome/schema.xsd"
"xmloutFN" ia a string in the form "/home/myHome/toValidate.xml"

I read older posts and this seems to be what people did there. And it
seemed to work fine for them.
??

Side question: in case I do provoke errors on purpose (giving a non
well-formed xml file), the best I was able to do was to dereferenciate the
xercesc_3_1::SAXParseException.getMessage() which is most uninformative (I
get a most uninformative integer value). Any suggestions here?

Thank you for your help and patience!

Mattia

Re: force validation against schema (.xsd)

Posted by Chris Hillery <ce...@lambda.nu>.
You've actually done everything right, I believe, except that you haven't
set up an ErrorHandler to get notifications about what goes wrong at parse
time. Xerces doesn't throw exceptions or otherwise fail the call to parse()
unless something goes seriously wrong; instead, it sends notes to the
ErrorHandler. The DefaultHandler you're instantiating basically ignores all
errors. You need to create your own derived class to receive the error
notifications.

Take a look at the Xerces sample "SCMPrint", which is basically doing just
what you're doing here - it uses the SAX parser rather than DOM, but the
ErrorHandler interface is the same. You can copy and adapt their
"SCMPrintHandler" class to get the error notifications you're interested in.
Also see their call to "getSawErrors()" after calling parse().

This should also hopefully solve your second question about getting more
details about what goes wrong during parsing.

Good luck,
Ceej
aka Chris Hillery

On Wed, Sep 15, 2010 at 6:51 AM, <to...@sbc.su.se> wrote:

> Hi xerces users.
> I have a (hopefully) easy question for you experts out there.
> I would like to validate my xml file against an xml schema.
> The parser complains if my xml is ill-formed but does not validate against
> the schema I embedded into the xml. I tried to provide an external
> location explicitly but it makes no difference.
> (this should be pretty standard, but I can't figure it out...)
>
> I use the following code:
>
>    XMLPlatformUtils::Initialize();
>    XercesDOMParser* parser = new XercesDOMParser();
>    parser->setValidationScheme(XercesDOMParser::Val_Always);
>    parser->setDoSchema(true);
>    parser->setDoNamespaces(true);
>    parser->setExternalSchemaLocation(schema.c_str());
>    parser->loadGrammar(schema.c_str(), Grammar::SchemaGrammarType, true);
>    parser->setSkipDTDValidation(false);
>    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
>    parser->setErrorHandler(errHandler);
>    const char* xmlFile = xmloutFN.c_str();
>    try{
>      parser->parse(xmloutFN.c_str());
>    }
>
> where:
> "schema" is a string in the form "myNamespace /home/myHome/schema.xsd"
> "xmloutFN" ia a string in the form "/home/myHome/toValidate.xml"
>
> I read older posts and this seems to be what people did there. And it
> seemed to work fine for them.
> ??
>
> Side question: in case I do provoke errors on purpose (giving a non
> well-formed xml file), the best I was able to do was to dereferenciate the
> xercesc_3_1::SAXParseException.getMessage() which is most uninformative (I
> get a most uninformative integer value). Any suggestions here?
>
> Thank you for your help and patience!
>
> Mattia
>