Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2018 20:36
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 anonymous/2a0995ffd42a2ec132d6c69dacb63e75 to your computer and use it in GitHub Desktop.
Save anonymous/2a0995ffd42a2ec132d6c69dacb63e75 to your computer and use it in GitHub Desktop.
<foo>
<bar example="hello"/>
</foo>
#include <stdio.h>
#include <libxml/xpath.h>
int main(int argc, char **argv)
{
xmlDocPtr doc;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr obj;
doc = xmlReadFile("document.xml", NULL, 0);
ctx = xmlXPathNewContext(doc);
obj = xmlXPathEvalExpression(BAD_CAST "/foo/bar/@example", ctx);
if (!xmlXPathNodeSetIsEmpty(obj->nodesetval)) {
xmlChar *value = xmlNodeGetContent(obj->nodesetval->nodeTab[0]);
puts((char *) value);
xmlFree(value);
}
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctx);
xmlFreeDoc(doc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment