Home | History | Annotate | Download | only in tutorial
      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>
      2       </p><pre class="programlisting">
      3 #include &lt;stdio.h&gt;
      4 #include &lt;string.h&gt;
      5 #include &lt;stdlib.h&gt;
      6 #include &lt;libxml/xmlmemory.h&gt;
      7 #include &lt;libxml/parser.h&gt;
      8 
      9 void
     10 getReference (xmlDocPtr doc, xmlNodePtr cur) {
     11 
     12 	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 }
     24 
     25 
     26 void
     27 parseDoc(char *docname) {
     28 
     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-&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 }
     57 
     58 int
     59 main(int argc, char **argv) {
     60 
     61 	char *docname;
     62 
     63 	if (argc &lt;= 1) {
     64 		printf("Usage: %s docname\n", argv[0]);
     65 		return(0);
     66 	}
     67 
     68 	docname = argv[1];
     69 	parseDoc (docname);
     70 	
     71 	return (1);
     72 }
     73 
     74 </pre><p>
     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>
     76