Tree/FAQ
From Code Synthesis Wiki
Contents |
General
Parsing
How do I parse an XML instance to a Xerces-C++ DOM document?
While this question is not exactly about XSD or the C++/Tree mapping and it is covered in the Xerces-C++ Programming Guide, this step is a prerequisite to some more advanced techniques covered in this FAQ. Furthermore, the XSD runtime provides some untilities that make the code a little bit more palatable.
#include <istream> #include <xercesc/dom/DOM.hpp> #include <xercesc/framework/Wrapper4InputSource.hpp> #include <xsd/cxx/xml/dom/elements.hxx> #include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx> #include <xsd/cxx/xml/sax/std-input-source.hxx> #include <xsd/cxx/tree/exceptions.hxx> #include <xsd/cxx/tree/error-handler.hxx> xsd::cxx::xml::dom::auto_ptr<xercesc::DOMDocument> parse (std::istream& is) { using namespace xercesc; namespace xml = xsd::cxx::xml; namespace tree = xsd::cxx::tree; const bool validate (true); const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull}; // Get an implementation of the Load-Store (LS) interface. // DOMImplementationLS* impl ( static_cast<DOMImplementationLS*>( DOMImplementationRegistry::getDOMImplementation(ls_id))); // Create a DOMBuilder. // xml::dom::auto_ptr<DOMBuilder> parser ( impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0)); // Discard comment nodes in the document. // parser->setFeature (XMLUni::fgDOMComments, false); // Enable datatype normalization. // parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true); // Do not create EntityReference nodes in the DOM tree. No // EntityReference nodes will be created, only the nodes // corresponding to their fully expanded substitution text // will be created. // parser->setFeature (XMLUni::fgDOMEntities, false); // Perform Namespace processing. // parser->setFeature (XMLUni::fgDOMNamespaces, true); // Do not include ignorable whitespace in the DOM tree. // parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false); // Enable/Disable validation. // parser->setFeature (XMLUni::fgDOMValidation, validate); parser->setFeature (XMLUni::fgXercesSchema, validate); parser->setFeature (XMLUni::fgXercesSchemaFullChecking, validate); // We will release the DOM document ourselves. // parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true); // Set error handler. // tree::error_handler<char> eh; xml::dom::bits::error_handler_proxy<char> ehp (eh); parser->setErrorHandler (&ehp); // Prepare input stream. // xml::sax::std_input_source isrc (is); Wrapper4InputSource wrap (&isrc, false); xml::dom::auto_ptr<DOMDocument> doc (parser->parse (wrap)); eh.throw_if_failed<tree::parsing<char> > (); return doc; }
Below is a simple program that uses the above code.
#include <fstream> int main (int argc, char* argv[]) { using namespace xercesc; XMLPlatformUtils::Initialize (); std::ifstream ifs (argv[1]); parse (ifs); XMLPlatformUtils::Terminate (); }