views:

288

answers:

1

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?

A: 

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

Eugen Constantin Dinca
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 declaration
petersohn
@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.
Eugen Constantin Dinca
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?
petersohn
Eugen Constantin Dinca