Validating document in Xerces C++
Asked Answered
H

1

0

I want to load an XML document in Xerces-C++ (version 2.8, under Linux), and validate it using a DTD schema not referenced from the document. I tried the following:

XercesDOMParser parser;
parser.loadGrammar("grammar.dtd", Grammar::DTDGrammarType);
parser.setValidationScheme(XercesDOMParser::Val_Always);
parser.parse("xmlfile.xml");

But it doesn't indicate an error if the document is not valid. What am I missing?

Heartwarming answered 16/3, 2010 at 14:21 Comment(0)
A
2

You'll need to set an error handler before calling parse if you want to see anything:

Handler handler;    
parser.setErrorHandler( &handler );

where Handler is a class derived from ErrorHandler

Avaunt answered 16/3, 2010 at 15:25 Comment(6)
I added the error handler to the code. Now the error reporting works. However, when parsing my DTD file, I get an error. Here is my DTD file: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE root [ <!ELEMENT root (elem1*)> <!ATTLIST root attr1 CDATA> <!ATTLIST root attr2 CDATA #REQUIRED> <!ELEMENT elem1 (elem2*)> <!ATTLIST elem1 id CDATA #REQUIRED> <!ELEMENT elem2 (#PCDATA)> The error message: fatal error: grammar.dtd: 2,3: Expected a markup declarationHeartwarming
@petersohn: your DTD doesn't specify what kind of attribute attr1 is [value|REQUIRED|IMPLIED|FIXED] and doesn't seem to have the ending ]> Anyway, you can try to embed your grammar.dtd into xmlfile.xml and open the XML with XMLNotepad or similar.Avaunt
The DTD has an ending, I just forgot to copy-paste it here. Anyway, adding #REQUIRED to attr1 doesn't help, it gives the same error message. I have specific reason not to include the DTD in the XML file. Is it not possible to link the DTD to the XML programatically?Heartwarming
Including the DTD in the XML and opening it up with an XML editor was for debugging purposes, usually such an editor will give you more meaningful error messages. Paste the XML & the DTD files into pastebin.com or similar and put the links here.Avaunt
@EugenConstantinDinca how do you declare the Handler? I keep getter "handler is not defined in this scope" – TopGunCoder 6 mins agoWarden
@TopGunCoder: like it says in the answer "where Handler is a class derived from ErrorHandler". A random example from the internets: sivachandranp.wordpress.com/2010/10/10/…. HTHAvaunt

© 2022 - 2024 — McMap. All rights reserved.