Created
February 21, 2012 00:33
-
-
Save ender672/1872559 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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