Here is a working example of evaluating XPath with Xerces 3.1.2 .
XML example
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <root> <ApplicationSettings>hello world</ApplicationSettings> </root>
C ++
#include <iostream> #include <xercesc/dom/DOM.hpp> #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMElement.hpp> #include <xercesc/util/TransService.hpp> #include <xercesc/parsers/XercesDOMParser.hpp> using namespace xercesc; using namespace std; int main() { XMLPlatformUtils::Initialize(); // create the DOM parser XercesDOMParser *parser = new XercesDOMParser; parser->setValidationScheme(XercesDOMParser::Val_Never); parser->parse("sample.xml"); // get the DOM representation DOMDocument *doc = parser->getDocument(); // get the root element DOMElement* root = doc->getDocumentElement(); // evaluate the xpath DOMXPathResult* result=doc->evaluate( XMLString::transcode("/root/ApplicationSettings"), root, NULL, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL); if (result->getNodeValue() == NULL) { cout << "There is no result for the provided XPath " << endl; } else { cout<<TranscodeToStr(result->getNodeValue()->getFirstChild()->getNodeValue(),"ascii").str()<<endl; } XMLPlatformUtils::Terminate(); return 0; }
Compile and run (assumes a standard installation of the xerces library and a C ++ file called xpath.cpp)
g++ -g -Wall -pedantic -L/opt/lib -I/opt/include -DMAIN_TEST xpath.cpp -o xpath -lxerces-c ./xpath
Result
hello world
Mike s
source share