You want to represent an XML document as a C++ object so that you can manipulate its elements, attributes, text, DTD, processing instructions, and comments.
Use Xerces's implementation of the W3C DOM. First, use the class
xercesc::DOMImplementationRegistry to obtain an instance of
xercesc::DOMImplementation, then use the
DOMImplementation to create an instance of the parser
xercesc::DOMBuilder. Next, register an instance of
xercesc::DOMErrorHandler to receive notifications of parsing
errors, and invoke the parser's
parseURI() method with
your XML document's URI or file pathname as its argument. If the parse is successful,
parseURI will return a pointer to a
DOMDocument representing the XML document. You can then use
the functions defined by the W3C DOM specification to inspect and manipulate the
When you are done manipulating the document, you can save it to a file by obtaining a
DOMWriter from the
DOMImplementation and calling its
writeNode() method with a pointer to the
DOMDocument as its argument.
Example 14-10. Using DOM to load, modify, and then save an XML document
#include <exception> #include <iostream> // cout #include <xercesc/dom/DOM.hpp> #include <xercesc/framework/LocalFileFormatTarget.hpp> #include <xercesc/sax/SAXException.hpp> ...