VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.31/doc/tutorial/includexpath.c@ 49962

Last change on this file since 49962 was 39915, checked in by vboxsync, 13 years ago

libxml-2.6.31 unmodified

  • Property svn:eol-style set to native
File size: 1.5 KB
Line 
1<![CDATA[
2#include <libxml/parser.h>
3#include <libxml/xpath.h>
4
5xmlDocPtr
6getdoc (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
18xmlXPathObjectPtr
19getnodeset (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}
42int
43main(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]]>
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette