apb.html revision be98b3323fe8b32be04bf418caafd9f0fcc96385
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<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> 3 <pre class="programlisting"> 4#include <stdio.h> 5#include <string.h> 6#include <stdlib.h> 7#include <libxml/xmlmemory.h> 8#include <libxml/parser.h> 9 10void 11parseStory (xmlDocPtr doc, xmlNodePtr cur) { 12 13 cur = cur->xmlChildrenNode; 14 while (cur != NULL) { 15 if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) { 16 printf("keyword: %s\n", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)); 17 } 18 cur = cur->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,"Document not parsed successfully. \n"); 33 xmlFreeDoc(doc); 34 return; 35 } 36 37 cur = xmlDocGetRootElement(doc); 38 39 if (cur == NULL) { 40 fprintf(stderr,"empty document\n"); 41 xmlFreeDoc(doc); 42 return; 43 } 44 45 if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { 46 fprintf(stderr,"document of the wrong type, root node != story"); 47 xmlFreeDoc(doc); 48 return; 49 } 50 51 cur = cur->xmlChildrenNode; 52 while (cur != NULL) { 53 if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ 54 parseStory (doc, cur); 55 } 56 57 cur = cur->next; 58 } 59 60 xmlFreeDoc(doc); 61 return; 62} 63 64int 65main(int argc, char **argv) { 66 67 char *docname; 68 69 if (argc <= 1) { 70 printf("Usage: %s docname\n", 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> 82