Annotation of embedaddon/libxml2/doc/tutorial/includexpath.c, revision 1.1.1.1
1.1 misho 1: <![CDATA[
2: #include <libxml/parser.h>
3: #include <libxml/xpath.h>
4:
5: xmlDocPtr
6: getdoc (char *docname) {
7: xmlDocPtr doc;
8: doc = xmlParseFile(docname);
9:
10: if (doc == NULL ) {
11: fprintf(stderr,"Document not parsed successfully. \n");
12: return NULL;
13: }
14:
15: return doc;
16: }
17:
18: xmlXPathObjectPtr
19: getnodeset (xmlDocPtr doc, xmlChar *xpath){
20:
21: xmlXPathContextPtr context;
22: xmlXPathObjectPtr result;
23:
24: context = xmlXPathNewContext(doc);
25: if (context == NULL) {
26: printf("Error in xmlXPathNewContext\n");
27: return NULL;
28: }
29: result = xmlXPathEvalExpression(xpath, context);
30: xmlXPathFreeContext(context);
31: if (result == NULL) {
32: printf("Error in xmlXPathEvalExpression\n");
33: return NULL;
34: }
35: if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
36: xmlXPathFreeObject(result);
37: printf("No result\n");
38: return NULL;
39: }
40: return result;
41: }
42: int
43: main(int argc, char **argv) {
44:
45: char *docname;
46: xmlDocPtr doc;
47: xmlChar *xpath = (xmlChar*) "//keyword";
48: xmlNodeSetPtr nodeset;
49: xmlXPathObjectPtr result;
50: int i;
51: xmlChar *keyword;
52:
53: if (argc <= 1) {
54: printf("Usage: %s docname\n", argv[0]);
55: return(0);
56: }
57:
58: docname = argv[1];
59: doc = getdoc(docname);
60: result = getnodeset (doc, xpath);
61: if (result) {
62: nodeset = result->nodesetval;
63: for (i=0; i < nodeset->nodeNr; i++) {
64: keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
65: printf("keyword: %s\n", keyword);
66: xmlFree(keyword);
67: }
68: xmlXPathFreeObject (result);
69: }
70: xmlFreeDoc(doc);
71: xmlCleanupParser();
72: return (1);
73: }
74: ]]>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>