MDT 2002 John Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
MDT 2002 John Fleck | 77e4d35 | 2002-09-01 01:37:11 +0000 | [diff] [blame] | 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> |
MDT 2002 John Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 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 | |
| 10 | void |
| 11 | parseStory (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 | |
| 23 | static void |
| 24 | parseDoc(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"); |
John Fleck | be98b33 | 2002-09-04 03:16:23 +0000 | [diff] [blame^] | 33 | xmlFreeDoc(doc); |
MDT 2002 John Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 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 | } |
MDT 2002 John Fleck | 77e4d35 | 2002-09-01 01:37:11 +0000 | [diff] [blame] | 59 | |
| 60 | xmlFreeDoc(doc); |
| 61 | return; |
MDT 2002 John Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int |
| 65 | main(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> |