Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath support in Xerces-C

I am supporting a legacy C++ application which uses Xerces-C for XML parsing. I've been spoiled by .Net and am used to using XPath to select nodes from a DOM tree.

Is there any way to get access some limited XPath functionality in Xerces-C? I'm looking for something like selectNodes("/for/bar/baz"). I could do this manually, but XPath is so nice by comparison.

like image 867
Adam Tegen Avatar asked Jul 09 '09 19:07

Adam Tegen


3 Answers

See the xerces faq.

http://xerces.apache.org/xerces-c/faq-other-2.html#faq-9

Does Xerces-C++ support XPath? No.Xerces-C++ 2.8.0 and Xerces-C++ 3.0.1 only have partial XPath implementation for the purposes of handling Schema identity constraints. For full XPath support, you can refer Apache Xalan C++ or other Open Source Projects like Pathan.

It's fairly easy to do what you want using xalan however.

like image 99
hookenz Avatar answered Nov 03 '22 22:11

hookenz


Here is a working example of XPath evaluation with Xerces 3.1.2.

Sample XML

<?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 standard xerces library installation and C++ file named xpath.cpp)

g++ -g -Wall -pedantic -L/opt/lib -I/opt/include -DMAIN_TEST xpath.cpp -o xpath -lxerces-c
./xpath

Result

hello world
like image 27
Mike S Avatar answered Nov 03 '22 23:11

Mike S


According to the FAQ, Xerces-C supports partial XPath 1 implementation:

The same engine is made available through the DOMDocument::evaluate API to let the user perform simple XPath queries involving DOMElement nodes only, with no predicate testing and allowing the "//" operator only as the initial step.

You use DOMDocument::evaluate() to evaluate the expression, which then returns a DOMXPathResult.

like image 2
Jeff L Avatar answered Nov 03 '22 22:11

Jeff L