You are hereApache Xerces' weird error message

Apache Xerces' weird error message


By Gerd Riesselmann - Posted on 04 April 2007

For one of my projects I'm using Apache's Xerces C++ XML Parser and run into this strange error:

The primary document entity could not be opened.

Took me a while to figure out that Xerces simply wants to say "File not found" here, because I was was calling parse() with the data to be parsed, while Xerces expects a file name here. Silly me.

However: Here's how to parse an XML string using Xerces SAX parser. you need to use a MemBufInputSource:


std::string xmldoc("Some Text");
//
// Create a SAX parser object.
//
XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader* parser =
XERCES_CPP_NAMESPACE_QUALIFIER XMLReaderFactory::createXMLReader();
//
// Create the handler object and install it
//
YourContentHandler handler();
parser->setContentHandler(&handler);
parser->setErrorHandler(&handler);
XERCES_CPP_NAMESPACE_QUALIFIER MemBufInputSource source(
(const XMLByte*)xmldoc.c_str(),
xmldoc.length(),
"a name for the input source",
false
);
parser->parse(source);