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?

petersohn
  • 11,292
  • 13
  • 61
  • 98

1 Answers1

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

Eugen Constantin Dinca
  • 8,994
  • 2
  • 34
  • 51
  • 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: <!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 Mar 16 '10 at 16:27
  • @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 Mar 16 '10 at 20:06
  • 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 Mar 17 '10 at 07:51
  • 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 http://pastebin.com or similar and put the links here. – Eugen Constantin Dinca Mar 17 '10 at 15:31
  • @EugenConstantinDinca how do you declare the Handler? I keep getter "handler is not defined in this scope" – TopGunCoder 6 mins ago – CoderDake Feb 04 '13 at 20:10
  • 1
    @TopGunCoder: like it says in the answer "where Handler is a class derived from ErrorHandler". A random example from the internets: http://sivachandranp.wordpress.com/2010/10/10/xml-schema-validation-using-xerces-c/. HTH – Eugen Constantin Dinca Feb 05 '13 at 00:25