blob: fffa0eed68fc13b02fafa5cc579c26e39d876f8d [file] [log] [blame]
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +00001<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 Fleck8aff3b72003-04-26 03:54:07 +00002 </p><pre class="programlisting">
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +00003#include &lt;stdio.h&gt;
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +00004#include &lt;string.h&gt;
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +00005#include &lt;stdlib.h&gt;
6#include &lt;libxml/xmlmemory.h&gt;
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +00007#include &lt;libxml/parser.h&gt;
8
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +00009void
10getReference (xmlDocPtr doc, xmlNodePtr cur) {
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000011
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000012 xmlChar *uri;
13 cur = cur-&gt;xmlChildrenNode;
14 while (cur != NULL) {
15 if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"reference"))) {
16 uri = xmlGetProp(cur, "uri");
17 printf("uri: %s\n", uri);
18 xmlFree(uri);
19 }
20 cur = cur-&gt;next;
21 }
22 return;
23}
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000024
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000025
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000026void
27parseDoc(char *docname) {
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000028
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000029 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-&gt;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 Fleck8aff3b72003-04-26 03:54:07 +000057
58int
59main(int argc, char **argv) {
60
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000061 char *docname;
62
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000063 if (argc &lt;= 1) {
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000064 printf("Usage: %s docname\n", argv[0]);
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000065 return(0);
66 }
67
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000068 docname = argv[1];
69 parseDoc (docname);
70
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000071 return (1);
72}
73
74</pre><p>
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000075 </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>