apd.html revision 4c3bb7d8a20c5262882af7fcd41afe3b3d482f12
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;
5
6xmlDocPtr
7getdoc (char *docname) {
8	xmlDocPtr doc;
9	doc = xmlParseFile(docname);
10	
11	if (doc == NULL ) {
12		fprintf(stderr,"Document not parsed successfully. \n");
13		return NULL;
14	}
15
16	return doc;
17}
18
19xmlXPathObjectPtr
20getnodeset (xmlDocPtr doc, xmlChar *xpath){
21	
22	xmlXPathContextPtr context;
23	xmlXPathObjectPtr result;
24
25	context = xmlXPathNewContext(doc);
26	if (context == NULL) {
27		printf("Error in xmlXPathNewContext\n");
28		return NULL;
29	}
30	result = xmlXPathEvalExpression(xpath, context);
31	xmlXPathFreeContext(context);
32	if (result == NULL) {
33		printf("Error in xmlXPathEvalExpression\n");
34		return NULL;
35	}
36	if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
37		xmlXPathFreeObject(result);
38                printf("No result\n");
39		return NULL;
40	}
41	return result;
42}
43int
44main(int argc, char **argv) {
45
46	char *docname;
47	xmlDocPtr doc;
48	xmlChar *xpath = (xmlChar*) "//keyword";
49	xmlNodeSetPtr nodeset;
50	xmlXPathObjectPtr result;
51	int i;
52	xmlChar *keyword;
53		
54	if (argc &lt;= 1) {
55		printf("Usage: %s docname\n", argv[0]);
56		return(0);
57	}
58
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);
68		}
69		xmlXPathFreeObject (result);
70	}
71	xmlFreeDoc(doc);
72	xmlCleanupParser();
73	return (1);
74}
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>
77