#include <libxml/parser.h> #include <libxml/xpath.h> xmlDocPtr getdoc (char *docname) { xmlDocPtr doc; doc = xmlParseFile(docname); if (doc == NULL ) { fprintf(stderr,"Document not parsed successfully. \n"); return NULL; } return doc; } xmlXPathObjectPtr getnodeset (xmlDocPtr doc){ xmlXPathContextPtr context; xmlChar *xpath; xmlXPathObjectPtr result; xpath = ("//keyword"); context = xmlXPathNewContext(doc); result = xmlXPathEvalExpression(xpath, context); if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ printf("No result\n"); return NULL; } return result; } int main(int argc, char **argv) { char *docname; xmlDocPtr doc; xmlNodeSetPtr nodeset; xmlXPathObjectPtr result; int i; xmlChar *keyword; if (argc <= 1) { printf("Usage: %s docname\n", argv[0]); return(0); } docname = argv[1]; doc = getdoc(docname); result = getnodeset (doc); if (result) { nodeset = result->nodesetval; for (i=0; i < nodeset->nodeNr; i++) { keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); printf("keyword: %s\n", keyword); } } xmlXPathFreeObject (result); return (1); }