开发者

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

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?开发者_运维百科


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜