You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Frank Zhou <fc...@yahoo.com> on 2006/05/18 06:04:55 UTC

validate XML against DTD using SAX2XMLReader

Hi All,

I am using Xerces-c 2.7.  I am having a problem
validating XML against a given DTD (a string).

I am using SAX2XMLReader and using loadGrammar(...) to
load the DTD. My code is something like this:

    SAX2XMLReader* parser =
XMLReaderFactory::createXMLReader();
    parser->setFeature(XMLUni::fgSAX2CoreValidation,
m_bValidate);
    parser->setFeature(XMLUni::fgXercesDynamic, true);

    parser->loagGrammar(DTDInputSource,
Grammar::DTDGrammarType);
    
    parser->setErrorHandler(errorHandler);
    parser->setContentHandler(contentHandler);

    parser->parse(XMLinputsource);

I noticed that the validation is not happening,
invalid xml gets parsed without problem. However, if I
don't use loadGrammar(...), instead, I embedded the
dtd content into XML input using <!DOCTYPE ... ]>,
then the validation happens correctly. 

Do I need to do some special settings? any problem
with loadGrammar(...)? 

Thanks much.
Frank




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


Re: validate XML against DTD using SAX2XMLReader

Posted by Frank Zhou <fc...@yahoo.com>.
David,

I tried that. By setting XMLUni::fgXercesDynamic to
false, the parse will report an error on the root
element of the xml instance (the root element is a
valid one), and setting it to true, it will report no
error even if there is one. 

What is the method loagGrammar(...) used for? Is it
supposed to work for DTD validation purpose? Or I have
to embed the DTD into the XML instance, which is very
awkward to me.

Thanks.
Frank

--- David Bertoni <db...@apache.org> wrote:

> Frank Zhou wrote:
> > David,
> > 
> > Thanks for your reply. I added the two features
> like
> > this:
> > 
> > 
> >
>
pParser->setFeature(XMLUni::fgXercesCacheGrammarFromParse,
> > true);
> > 
> >
>
pParser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse,
> > true);
> > 
> > And I am seeing the same behavior. Is there any
> sample
> > code snappet available anywhere? I could'nt find
> any
> > in the samples provided by Xerces-c package.
> > 
> 
> You probably need to set the following:
> 
> parser->setFeature(XMLUni::fgXercesDynamic, false);
> 
> Dynamic validation means the parser will only
> validate if there is a 
> DOCTYPE declaration indicating there's a grammar.
> 
> Take a look at the SAX2Print sample for more
> information.
> 
> Dave
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> c-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail:
> c-dev-help@xerces.apache.org
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


Re: validate XML against DTD using SAX2XMLReader

Posted by David Bertoni <db...@apache.org>.
Frank Zhou wrote:
> David,
> 
> Thanks for your reply. I added the two features like
> this:
> 
> 
> pParser->setFeature(XMLUni::fgXercesCacheGrammarFromParse,
> true);
> 
> pParser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse,
> true);
> 
> And I am seeing the same behavior. Is there any sample
> code snappet available anywhere? I could'nt find any
> in the samples provided by Xerces-c package.
> 

You probably need to set the following:

parser->setFeature(XMLUni::fgXercesDynamic, false);

Dynamic validation means the parser will only validate if there is a 
DOCTYPE declaration indicating there's a grammar.

Take a look at the SAX2Print sample for more information.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


Re: validate XML against DTD using SAX2XMLReader

Posted by Frank Zhou <fc...@yahoo.com>.
David,

Thanks for your reply. I added the two features like
this:


pParser->setFeature(XMLUni::fgXercesCacheGrammarFromParse,
true);

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

And I am seeing the same behavior. Is there any sample
code snappet available anywhere? I could'nt find any
in the samples provided by Xerces-c package.

Thanks again.
Frank  

--- David Bertoni <db...@apache.org> wrote:

> Frank Zhou wrote:
> > Hi All,
> > 
> > I am using Xerces-c 2.7.  I am having a problem
> > validating XML against a given DTD (a string).
> > 
> > I am using SAX2XMLReader and using
> loadGrammar(...) to
> > load the DTD. My code is something like this:
> > 
> >     SAX2XMLReader* parser =
> > XMLReaderFactory::createXMLReader();
> >    
> parser->setFeature(XMLUni::fgSAX2CoreValidation,
> > m_bValidate);
> >     parser->setFeature(XMLUni::fgXercesDynamic,
> true);
> > 
> >     parser->loagGrammar(DTDInputSource,
> > Grammar::DTDGrammarType);
> >     
> >     parser->setErrorHandler(errorHandler);
> >     parser->setContentHandler(contentHandler);
> > 
> >     parser->parse(XMLinputsource);
> > 
> > I noticed that the validation is not happening,
> > invalid xml gets parsed without problem. However,
> if I
> > don't use loadGrammar(...), instead, I embedded
> the
> > dtd content into XML input using <!DOCTYPE ... ]>,
> > then the validation happens correctly. 
> > 
> > Do I need to do some special settings? any problem
> > with loadGrammar(...)? 
> > 
> 
> I believe you need to set the feature "use cached
> grammar in parse:"
> 
>
http://xml.apache.org/xerces-c/program-sax2.html#use-cached
> 
> Not that if you enable the feature to cache the
> grammar from the parse, 
> this feature is automatically enabled:
> 
>
http://xml.apache.org/xerces-c/program-sax2.html#cache-grammar
> 
> Dave
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> c-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail:
> c-dev-help@xerces.apache.org
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


Re: validate XML against DTD using SAX2XMLReader

Posted by David Bertoni <db...@apache.org>.
Frank Zhou wrote:
> Hi All,
> 
> I am using Xerces-c 2.7.  I am having a problem
> validating XML against a given DTD (a string).
> 
> I am using SAX2XMLReader and using loadGrammar(...) to
> load the DTD. My code is something like this:
> 
>     SAX2XMLReader* parser =
> XMLReaderFactory::createXMLReader();
>     parser->setFeature(XMLUni::fgSAX2CoreValidation,
> m_bValidate);
>     parser->setFeature(XMLUni::fgXercesDynamic, true);
> 
>     parser->loagGrammar(DTDInputSource,
> Grammar::DTDGrammarType);
>     
>     parser->setErrorHandler(errorHandler);
>     parser->setContentHandler(contentHandler);
> 
>     parser->parse(XMLinputsource);
> 
> I noticed that the validation is not happening,
> invalid xml gets parsed without problem. However, if I
> don't use loadGrammar(...), instead, I embedded the
> dtd content into XML input using <!DOCTYPE ... ]>,
> then the validation happens correctly. 
> 
> Do I need to do some special settings? any problem
> with loadGrammar(...)? 
> 

I believe you need to set the feature "use cached grammar in parse:"

http://xml.apache.org/xerces-c/program-sax2.html#use-cached

Not that if you enable the feature to cache the grammar from the parse, 
this feature is automatically enabled:

http://xml.apache.org/xerces-c/program-sax2.html#cache-grammar

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org