Search code examples
c++xpathlibxml2

Determine Xpath content via libxml


I found this following XML example but am not sure how the xpath data can be obtained in C++ (besides using boost ptree xmlparser).

/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
if(xpathObj == NULL) {
    fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
    xmlXPathFreeContext(xpathCtx); 
    xmlFreeDoc(doc); 
    return;
}
//cout <<"REsult : "<<xpathObj->stringval<<endl; /* Fails with bus error */

Solution

  • What exactly is contained in xpathObj depends on what your xpath expression matches. If you're looking to just get text out of it, you can iterate over nodes in xpathObj->nodesetval, calling xmlNodeListGetString, as show in the example.

    http://www.xmlsoft.org/tutorial/ar01s05.html