Use Xerces-C++ as simple as possible
The following examples demonstrate how simple it is to work with XML DOM.
Load a XML document into the DOM:
int main( int argc, char* argv[] ) { // Get an implementation which is supporting "LS" (Load Save) DomImplementation impl = DomImplementationRegistry::getInstance().getDomImplementation( DomString( "LS" ) ); assert( ! impl.isNull() ); DomLSParser parser = impl.createLSParser( DomImplementation::ModeSynchronous, DomImplementation::AnySchema ); DomDocument doc = parser.parseURI( DomString( "d:\\test.xml" ) ); DomElement docElement = doc.getDocumentElement(); displayElement( docElement, 0 ); std::cout << "Press Enter:" << std::endl; std::cin.get(); return 0; }
Iterate trough elements in the DOM:
void displayElement( const DomElement& element, int indentationLevel ) { // indent the entry std::cout << std::string( indentationLevel * 2, ' ' ); // print the name of the node and it attributes. std::cout << element.getNodeName().toLatin1() << " ("; DomNamedNodeMap attributes = element.getAttributes(); // iterate trough all attributes for( unsigned int i = 0; i < attributes.getLength(); ++i ) { if( i > 0 ) std::cout << ", "; // Cast a node from the named node map into an attribute. DomAttribute attribute = attributes.getItem( i ).toAttribute(); assert( ! attribute.isNull() ); // Print the name and value of the attribute. std::cout << attribute.getName().toLatin1() << "=\"" << attribute.getValue().toLatin1() << "\""; // Note: it's possible to reach the goal without cast with the getNodeName() and // getNodeValue() methods directly from DomNode. } std::cout << ")" << std::endl; // iterate trough sub elements for( DomElement e = element.getFirstChildElement(); ! e.isNull(); e = e.getNextSiblingElement() ) { // recursive call displayElement( e, indentationLevel + 1 ); } }
Necessary includes for the examples above:
#include <assert.h> #include <iostream> #include <string> #include <XMLDOM/All.h> using namespace xmldom;
Find all the details on the sourceforge project page: XMLDOM Xecres Wrapper at Sourceforge
XMLDOM Xerces Wrapper is licensed under the GNU Lesser General Public License.
Copyright (C) 2007-2008 Bernafon AG http://www.bernafon.com.