blob: f7bcea094800cda03b33a1447ff3379c3a1dee92 [file] [log] [blame]
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
MDT 2002 John Fleck77e4d352002-09-01 01:37:11 +00002<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>B. Code for Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apa.html" title="A. Sample Document"><link rel="next" href="apc.html" title="C. 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">B. Code for Keyword Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apa.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="apc.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="keywordappendix"></a>B. Code for Keyword Example</h2><p>
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00003 <pre class="programlisting">
4#include &lt;stdio.h&gt;
5#include &lt;string.h&gt;
6#include &lt;stdlib.h&gt;
7#include &lt;libxml/xmlmemory.h&gt;
8#include &lt;libxml/parser.h&gt;
9
10void
11parseStory (xmlDocPtr doc, xmlNodePtr cur) {
12
13 cur = cur-&gt;xmlChildrenNode;
14 while (cur != NULL) {
15 if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;keyword&quot;))) {
16 printf(&quot;keyword: %s\n&quot;, xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1));
17 }
18 cur = cur-&gt;next;
19 }
20 return;
21}
22
23static void
24parseDoc(char *docname) {
25
26 xmlDocPtr doc;
27 xmlNodePtr cur;
28
29 doc = xmlParseFile(docname);
30
31 if (doc == NULL ) {
32 fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
John Fleckbe98b332002-09-04 03:16:23 +000033 xmlFreeDoc(doc);
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000034 return;
35 }
36
37 cur = xmlDocGetRootElement(doc);
38
39 if (cur == NULL) {
40 fprintf(stderr,&quot;empty document\n&quot;);
41 xmlFreeDoc(doc);
42 return;
43 }
44
45 if (xmlStrcmp(cur-&gt;name, (const xmlChar *) &quot;story&quot;)) {
46 fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
47 xmlFreeDoc(doc);
48 return;
49 }
50
51 cur = cur-&gt;xmlChildrenNode;
52 while (cur != NULL) {
53 if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;storyinfo&quot;))){
54 parseStory (doc, cur);
55 }
56
57 cur = cur-&gt;next;
58 }
MDT 2002 John Fleck77e4d352002-09-01 01:37:11 +000059
60 xmlFreeDoc(doc);
61 return;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000062}
63
64int
65main(int argc, char **argv) {
66
67 char *docname;
68
69 if (argc &lt;= 1) {
70 printf(&quot;Usage: %s docname\n&quot;, argv[0]);
71 return(0);
72 }
73
74 docname = argv[1];
75 parseDoc (docname);
76
77 return (1);
78}
79
80</pre>
81 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apa.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="apc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">A. Sample Document </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> C. Code for Add Keyword Example</td></tr></table></div></body></html>