mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-01-07 17:17:39 +03:00
d14bccc241
Sat Feb 14 18:55:40 MST 2004 John Fleck <jfleck@inkstain.net> * doc/tutorial/xmltutorial.xml * doc/tutorial/xmltutorial.pdf * doc/tutorial/*.html Fix bug in XPath example in the tutorial, thanks to Carlos, whose last name I don't know, for pointing this out
66 lines
1.3 KiB
C
66 lines
1.3 KiB
C
<![CDATA[
|
|
#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, xmlChar *xpath){
|
|
|
|
xmlXPathContextPtr context;
|
|
xmlXPathObjectPtr result;
|
|
|
|
context = xmlXPathNewContext(doc);
|
|
result = xmlXPathEvalExpression(xpath, context);
|
|
if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
|
|
printf("No result\n");
|
|
return NULL;
|
|
}
|
|
xmlXPathFreeContext(context);
|
|
return result;
|
|
}
|
|
int
|
|
main(int argc, char **argv) {
|
|
|
|
char *docname;
|
|
xmlDocPtr doc;
|
|
xmlChar *xpath = ("//keyword");
|
|
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, xpath);
|
|
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);
|
|
xmlFree(keyword);
|
|
}
|
|
xmlXPathFreeObject (result);
|
|
}
|
|
xmlFreeDoc(doc);
|
|
xmlCleanupParser();
|
|
return (1);
|
|
}
|
|
]]>
|