This example shows an example of running multiple queries, using a DOMXPathNSResolver to resolve namespaces.
#include <iostream>
#include <xercesc/framework/StdOutFormatTarget.hpp>
XERCES_CPP_NAMESPACE_USE;
using namespace std;
int main(int argc, char *argv[])
{
if(argc != 2) {
cerr << "Data file not specified." << endl;
return 1;
}
try {
} catch (const XMLException& eXerces) {
cerr << "Error during Xerces-C initialisation.\n"
<< "Xerces exception message: "
<< UTF8(eXerces.getMessage()) << endl;
return 1;
}
try {
DOMImplementation* xqillaImplementation =
DOMImplementationRegistry::getDOMImplementation(X("XPath2 3.0"));
parser->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, true);
parser->getDomConfig()->setParameter(XMLUni::fgXercesSchema, true);
parser->getDomConfig()->setParameter(XMLUni::fgDOMValidateIfSchema, true);
DOMDocument *document = parser->parseURI(argv[1]);
if(document == 0) {
cerr << "Document not found: " << argv[1] << endl;
return 1;
}
resolver->addNamespaceBinding(X("my"), X("http://example.com/myURI"));
{
const char* expression = "max(/Catalogue/Book/Price)";
AutoRelease<DOMXPathResult> firstResult(parsedExpression->evaluate(document->getDocumentElement(), DOMXPathResult::FIRST_RESULT_TYPE, 0));
cout << "The answer for expression '" << expression << "' is: " << firstResult->getNumberValue() << endl;
}
{
const char* expression2 = "//Magazine";
AutoRelease<DOMXPathResult> iteratorResult(parsedExpression->evaluate(document->getDocumentElement(), DOMXPathResult::ITERATOR_RESULT_TYPE, 0));
StdOutFormatTarget target;
output->setByteStream(&target);
cout << "The answer for expression '" << expression2 << "' is: " <<endl;
int i = 0;
while(iteratorResult->iterateNext()) {
if(iteratorResult->isNode()) {
cout << "Node " << i++ << ": "<< flush;
serializer->write(iteratorResult->getNodeValue(), output);
cout << endl;
} else {
cerr << "Expected a node but received an atomic value!"<< endl;
}
}
}
}
catch(DOMException &e) {
cerr << "DOMException: " << UTF8(e.getMessage()) << endl;
return 1;
}
return 0;
}