MST 2004 John Fleck | d14bccc | 2004-02-15 01:57:42 +0000 | [diff] [blame] | 1 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>G. Code for Retrieving Attribute Value 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="apf.html" title="F. Code for Add Attribute Example"><link rel="next" href="aph.html" title="H. Code for Encoding Conversion 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">G. Code for Retrieving Attribute Value Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apf.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="aph.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="getattributeappendix"></a>G. Code for Retrieving Attribute Value Example</h2></div></div><div></div></div><p> |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 2 | </p><pre class="programlisting"> |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 4 | #include <string.h> |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <libxml/xmlmemory.h> |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 7 | #include <libxml/parser.h> |
| 8 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 9 | void |
| 10 | getReference (xmlDocPtr doc, xmlNodePtr cur) { |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 11 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 12 | xmlChar *uri; |
| 13 | cur = cur->xmlChildrenNode; |
| 14 | while (cur != NULL) { |
| 15 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) { |
| 16 | uri = xmlGetProp(cur, "uri"); |
| 17 | printf("uri: %s\n", uri); |
| 18 | xmlFree(uri); |
| 19 | } |
| 20 | cur = cur->next; |
| 21 | } |
| 22 | return; |
| 23 | } |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 24 | |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 25 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 26 | void |
| 27 | parseDoc(char *docname) { |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 28 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 29 | xmlDocPtr doc; |
| 30 | xmlNodePtr cur; |
| 31 | |
| 32 | doc = xmlParseFile(docname); |
| 33 | |
| 34 | if (doc == NULL ) { |
| 35 | fprintf(stderr,"Document not parsed successfully. \n"); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | cur = xmlDocGetRootElement(doc); |
| 40 | |
| 41 | if (cur == NULL) { |
| 42 | fprintf(stderr,"empty document\n"); |
| 43 | xmlFreeDoc(doc); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { |
| 48 | fprintf(stderr,"document of the wrong type, root node != story"); |
| 49 | xmlFreeDoc(doc); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | getReference (doc, cur); |
| 54 | xmlFreeDoc(doc); |
| 55 | return; |
| 56 | } |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 57 | |
| 58 | int |
| 59 | main(int argc, char **argv) { |
| 60 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 61 | char *docname; |
| 62 | |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 63 | if (argc <= 1) { |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 64 | printf("Usage: %s docname\n", argv[0]); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 65 | return(0); |
| 66 | } |
| 67 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 68 | docname = argv[1]; |
| 69 | parseDoc (docname); |
| 70 | |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 71 | return (1); |
| 72 | } |
| 73 | |
| 74 | </pre><p> |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 75 | </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apf.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="aph.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F. Code for Add Attribute Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> H. Code for Encoding Conversion Example</td></tr></table></div></body></html> |