1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>C.Code for Keyword 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="apb.html" title="B.Sample Document"><link rel="next" href="apd.html" title="D.Code for XPath 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">C.Code for Keyword Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apb.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="apd.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="keywordappendix"></a>C.Code for Keyword Example</h2></div></div><div></div></div><p> 2 </p><pre class="programlisting"> 3 #include <stdio.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <libxml/xmlmemory.h> 7 #include <libxml/parser.h> 8 9 void 10 parseStory (xmlDocPtr doc, xmlNodePtr cur) { 11 12 xmlChar *key; 13 cur = cur->xmlChildrenNode; 14 while (cur != NULL) { 15 if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) { 16 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 17 printf("keyword: %s\n", key); 18 xmlFree(key); 19 } 20 cur = cur->next; 21 } 22 return; 23 } 24 25 static void 26 parseDoc(char *docname) { 27 28 xmlDocPtr doc; 29 xmlNodePtr cur; 30 31 doc = xmlParseFile(docname); 32 33 if (doc == NULL ) { 34 fprintf(stderr,"Document not parsed successfully. \n"); 35 return; 36 } 37 38 cur = xmlDocGetRootElement(doc); 39 40 if (cur == NULL) { 41 fprintf(stderr,"empty document\n"); 42 xmlFreeDoc(doc); 43 return; 44 } 45 46 if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { 47 fprintf(stderr,"document of the wrong type, root node != story"); 48 xmlFreeDoc(doc); 49 return; 50 } 51 52 cur = cur->xmlChildrenNode; 53 while (cur != NULL) { 54 if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ 55 parseStory (doc, cur); 56 } 57 58 cur = cur->next; 59 } 60 61 xmlFreeDoc(doc); 62 return; 63 } 64 65 int 66 main(int argc, char **argv) { 67 68 char *docname; 69 70 if (argc <= 1) { 71 printf("Usage: %s docname\n", argv[0]); 72 return(0); 73 } 74 75 docname = argv[1]; 76 parseDoc (docname); 77 78 return (1); 79 } 80 81 </pre><p> 82 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apb.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="apd.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">B.Sample Document</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">D.Code for XPath Example</td></tr></table></div></body></html> 83