blob: f61470ff36a627e2e8a86ebd6f360be75d4b41b5 [file] [log] [blame]
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +00001<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>
MST 2003 John Fleck731967e2003-01-27 00:39:50 +00002 </p><pre class="programlisting">
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00003#include &lt;libxml/parser.h&gt;
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +00004#include &lt;libxml/xpath.h&gt;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00005
6xmlDocPtr
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +00007getdoc (char *docname) {
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00008 xmlDocPtr doc;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00009 doc = xmlParseFile(docname);
10
11 if (doc == NULL ) {
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000012 fprintf(stderr,"Document not parsed successfully. \n");
13 return NULL;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000014 }
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000015
16 return doc;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000017}
18
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000019xmlXPathObjectPtr
20getnodeset (xmlDocPtr doc, xmlChar *xpath){
21
22 xmlXPathContextPtr context;
23 xmlXPathObjectPtr result;
24
25 context = xmlXPathNewContext(doc);
26 result = xmlXPathEvalExpression(xpath, context);
27 if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
28 printf("No result\n");
29 return NULL;
30 }
31 xmlXPathFreeContext(context);
32 return result;
33}
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000034int
35main(int argc, char **argv) {
36
37 char *docname;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000038 xmlDocPtr doc;
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000039 xmlChar *xpath = ("//keyword");
40 xmlNodeSetPtr nodeset;
41 xmlXPathObjectPtr result;
42 int i;
43 xmlChar *keyword;
44
45 if (argc &lt;= 1) {
46 printf("Usage: %s docname\n", argv[0]);
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000047 return(0);
48 }
49
50 docname = argv[1];
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000051 doc = getdoc(docname);
52 result = getnodeset (doc, xpath);
53 if (result) {
54 nodeset = result-&gt;nodesetval;
55 for (i=0; i &lt; nodeset-&gt;nodeNr; i++) {
56 keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1);
57 printf("keyword: %s\n", keyword);
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000058 xmlFree(keyword);
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000059 }
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000060 xmlXPathFreeObject (result);
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000061 }
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000062 xmlFreeDoc(doc);
63 xmlCleanupParser();
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000064 return (1);
65}
66
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000067</pre><p>
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000068 </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>