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 Miguel Tena <mi...@gtd.es> on 2006/03/03 13:09:06 UTC
Problem loading a grammar to check DTD
Hi,
I'm trying to validate a XML file against a DTD not specified inside the XML
(it doesn't have the common DOCTYPE declaration to indicate where lays the
DTD). I'm using the loadGrammar method the tell the parser which DTD file to
use. This method loads the grammar and everything seems to work fine but
when I do the parsing of the XML file the errors reported are about unknown
elements or not defined attributes, nothing about the actual structure
defined by the DTD. I've tried with SAX2, SAX and DOM parsers but I've found
the same problem. This is an example of my program:
// Initialize platform
XMLPlatformUtils::Initialize();
// Create Parser
SAX2XMLReader* parser =
XMLReaderFactory::createXMLReader();
//
//Parser options
//
// Enable grammar reuse
parser->setFeature(
xercesc::XMLUni::fgXercesUseCachedGrammarInParse, true);
// Required to validate against the DTD. Force the
checking of every element
parser->setFeature(
xercesc::XMLUni::fgSAX2CoreValidation, true);
// Don't stop the parsing after the first fatal
error
parser->setExitOnFirstFatalError(false);
// Allow to load an external DTD
parser->setFeature(
xercesc::XMLUni::fgXercesLoadExternalDTD, true);
// This will flush the grammar pool
parser->resetCachedGrammarPool();
// Set error handler, the error handler writes the error messages found
during the parsing into a file
OurErrorHandler errorHandler;
parser->setErrorHandler(&errorHandler);
errorHandler.resetErrors();
// Load grammar and cache it
Grammar* grammar = parser->loadGrammar( dtdFileName,
Grammar::DTDGrammarType, true);
//Parse XML file
parser->parse(fileName);
The version of Xerces is 2.70 and the compiler is gcc 2.95 working on a
Solaris 2.8.
Does anybody know if I need to specify another option to correctly load the
DTD grammar? Is it compulsory to specify the DTD file inside the XML with a
DOCTYPE declaration?
Thanks a lot.
Miguel