What's the most efficient way to do recursive XPath queries using libxml2?

依然范特西╮ 提交于 2019-12-07 09:26:33

问题


I've written a C++ wrapper function for libxml2 that makes it easy for me to do queries on an XML document:

bool XPathQuery(
    const std::string& doc,
    const std::string& query,
    XPathResults& results); 

But I have a problem: I need to be able to do another XPath query on the results of my first query.

Currently I do this by storing the entire subdocument in my XPathResult object, and then I pass XPathResult.subdoc into the XPathQuery function. This is awfully inefficient.

So I'm wondering ... does libxml2 provide anything that would make it easy to store the context of an xpath query (a reference to a node, perhaps?) and then perform another query using that reference as the xpath root?


回答1:


You should reuse the xmlXPathContext and just change its node member.

#include <stdio.h>
#include <libxml/xpath.h>
#include <libxml/xmlerror.h>

static xmlChar buffer[] = 
"<?xml version=\"1.0\"?>\n<foo><bar><baz/></bar></foo>\n";

int
main()
{
  const char *expr = "/foo";

  xmlDocPtr document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT);
  xmlXPathContextPtr ctx = xmlXPathNewContext(document);
  //ctx->node = xmlDocGetRootElement(document);

  xmlXPathCompExprPtr p = xmlXPathCtxtCompile(ctx, (xmlChar *)expr);
  xmlXPathObjectPtr res = xmlXPathCompiledEval(p, ctx);

  if (XPATH_NODESET != res->type)
    return 1;

  fprintf(stderr, "Got object from first query:\n");
  xmlXPathDebugDumpObject(stdout, res, 0);
  xmlNodeSetPtr ns = res->nodesetval;
  if (!ns->nodeNr)
    return 1;
  ctx->node = ns->nodeTab[0];
  xmlXPathFreeObject(res);

  expr = "bar/baz";
  p = xmlXPathCtxtCompile(ctx, (xmlChar *)expr);
  res = xmlXPathCompiledEval(p, ctx);

  if (XPATH_NODESET != res->type)
    return 1;
  ns = res->nodesetval;
  if (!ns->nodeNr)
    return 1;
  fprintf(stderr, "Got object from second query:\n");
  xmlXPathDebugDumpObject(stdout, res, 0);

  xmlXPathFreeContext(ctx);
  return 0;
}


来源:https://stackoverflow.com/questions/1683081/whats-the-most-efficient-way-to-do-recursive-xpath-queries-using-libxml2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!