libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / doc / tutorial / apd.html
blob8f9618d52b3608fa1468c3f728f264a91e092506
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>D. Code for XPath Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.61.2"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apc.html" title="C. Code for Keyword Example"><link rel="next" href="ape.html" title="E. Code for Add Keyword Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">D. Code for XPath Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ape.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="xpathappendix"></a>D. Code for XPath Example</h2></div></div><div></div></div><p>
2 </p><pre class="programlisting">
3 #include &lt;libxml/parser.h&gt;
4 #include &lt;libxml/xpath.h&gt;
6 xmlDocPtr
7 getdoc (char *docname) {
8 xmlDocPtr doc;
9 doc = xmlParseFile(docname);
11 if (doc == NULL ) {
12 fprintf(stderr,"Document not parsed successfully. \n");
13 return NULL;
16 return doc;
19 xmlXPathObjectPtr
20 getnodeset (xmlDocPtr doc, xmlChar *xpath){
22 xmlXPathContextPtr context;
23 xmlXPathObjectPtr result;
25 context = xmlXPathNewContext(doc);
26 if (context == NULL) {
27 printf("Error in xmlXPathNewContext\n");
28 return NULL;
30 result = xmlXPathEvalExpression(xpath, context);
31 xmlXPathFreeContext(context);
32 if (result == NULL) {
33 printf("Error in xmlXPathEvalExpression\n");
34 return NULL;
36 if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
37 xmlXPathFreeObject(result);
38 printf("No result\n");
39 return NULL;
41 return result;
43 int
44 main(int argc, char **argv) {
46 char *docname;
47 xmlDocPtr doc;
48 xmlChar *xpath = (xmlChar*) "//keyword";
49 xmlNodeSetPtr nodeset;
50 xmlXPathObjectPtr result;
51 int i;
52 xmlChar *keyword;
54 if (argc &lt;= 1) {
55 printf("Usage: %s docname\n", argv[0]);
56 return(0);
59 docname = argv[1];
60 doc = getdoc(docname);
61 result = getnodeset (doc, xpath);
62 if (result) {
63 nodeset = result-&gt;nodesetval;
64 for (i=0; i &lt; nodeset-&gt;nodeNr; i++) {
65 keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1);
66 printf("keyword: %s\n", keyword);
67 xmlFree(keyword);
69 xmlXPathFreeObject (result);
71 xmlFreeDoc(doc);
72 xmlCleanupParser();
73 return (1);
75 </pre><p>
76 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ape.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">C. Code for Keyword Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> E. Code for Add Keyword Example</td></tr></table></div></body></html>