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 Alain Leblanc <aa...@gmail.com> on 2008/01/16 06:29:22 UTC

Validating xml file with schema

Hi,

I'm just starting to use xerces so hopefully I'm just missing something obvious.

I have a schema file MySchema.xsd and a xml file example.xml and want
to validate and parse the XML file. I used xmllint (on Linux) to
verify that my schema is correct and that example.xml is valid
according the schema.

Here's the C++ code I wrote to perform this task with Xerces:

=============================================
 XMLPlatformUtils::Initialize();
...
DOMImplementation *impl = DOMImplementation::getImplementation();
DOMBuilder *parser = ((DOMImplementationLS *)impl)->
createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);

//parser->setFeature(XMLUni::fgXercesSchema, true);
//parser->setFeature(XMLUni::fgDOMNamespaces, true);
//parser->setFeature(XMLUni::fgDOMValidateIfSchema, true);

parser->setFeature(XMLUni::fgDOMValidation, true);
parser->loadGrammar("MySchema.xsd", Grammar::SchemaGrammarType, true);

DOMDocument *doc =  parser -> parseURI("example.xml");

=============================================

When I run this version (with the first 3 setFeature()'s commented
out) the parseURI function generates an "Unknown element" error for
every tag in example.xml. If I uncomment any of the 3 setFeatures the
the file is parsed correctly but does not validate according to the
schema.

I verified that the loadGrammar() reads and processes the schema
correctly by adding errors to my schema files and they would be
reported.

Does anyone has any idea for what I need to do?

Thanks,

Alain

Re: Validating xml file with schema

Posted by Alain Leblanc <aa...@gmail.com>.
Hi Boris,

Thanks for the suggestion, but it didn't change anything.

I'll keep trying, but I think I can achieve what I want for now using
XercesDOMParser so I won't spend too much time on this. I'll report
back if I figure out what I did wrong, though.

a


2008/1/16, Boris Kolpackov <bo...@codesynthesis.com>:
> Hi Alain,
>
> Alain Leblanc <aa...@gmail.com> writes:
>
> > //parser->setFeature(XMLUni::fgXercesSchema, true);
> > //parser->setFeature(XMLUni::fgDOMNamespaces, true);
> > //parser->setFeature(XMLUni::fgDOMValidateIfSchema, true);
> >
> > parser->setFeature(XMLUni::fgDOMValidation, true);
> > parser->loadGrammar("MySchema.xsd", Grammar::SchemaGrammarType, true);
> >
> > DOMDocument *doc =  parser -> parseURI("example.xml");
> >
> > [...]
> >
> > Does anyone has any idea for what I need to do?
>
> I think you need to add this:
>
> parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);
>
> Boris
>
> --
> Boris Kolpackov
> Code Synthesis Tools CC
> http://www.codesynthesis.com
> Open-Source, Cross-Platform C++ XML Data Binding
>

Re: Validating xml file with schema

Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Alain,

Alain Leblanc <aa...@gmail.com> writes:

> //parser->setFeature(XMLUni::fgXercesSchema, true);
> //parser->setFeature(XMLUni::fgDOMNamespaces, true);
> //parser->setFeature(XMLUni::fgDOMValidateIfSchema, true);
>
> parser->setFeature(XMLUni::fgDOMValidation, true);
> parser->loadGrammar("MySchema.xsd", Grammar::SchemaGrammarType, true);
>
> DOMDocument *doc =  parser -> parseURI("example.xml");
>
> [...]
>
> Does anyone has any idea for what I need to do?

I think you need to add this:

parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);

Boris

-- 
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding