Skip to content

Instantly share code, notes, and snippets.

@ender672
Created February 21, 2012 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ender672/1872559 to your computer and use it in GitHub Desktop.
Save ender672/1872559 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
void
main()
{
xmlDocPtr doc;
xmlNodePtr cur;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr xpath;
xmlNodeSetPtr set;
xmlChar *xml = "<foo></foo>";
xmlChar *query = "//namespace::*";
int i;
xmlInitParser();
xmlXPathInit();
doc = xmlReadMemory(xml, strlen(xml), NULL, NULL, XML_PARSE_RECOVER | XML_PARSE_NOERROR);
ctx = xmlXPathNewContext(doc);
ctx->node = doc->children;
xpath = xmlXPathEvalExpression(query, ctx);
set = xpath->nodesetval;
/* libxml expects the following to happen:
* xmlXPathFreeNodeSet(set);
* xmlFreeDoc(doc);
*/
/* We cannot guarantee the order of the above and the following can
* cause a segfault:
* xmlFreeDoc(doc);
* xmlXPathFreeNodeSet(set);
*/
/* Nokogiri currently does the following, but valgrind complains:
*/
xmlFreeDoc(doc);
for (i = 0; i < set->nodeNr; i++) {
cur = set->nodeTab[i];
if (cur && cur->type == XML_NAMESPACE_DECL)
xmlXPathNodeSetFreeNs(cur);
}
xmlXPathFreeNodeSetList(xpath);
xmlXPathFreeContext(ctx);
xmlFree(set->nodeTab);
xmlFree(set);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment