apg.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. void
  9. getReference (xmlDocPtr doc, xmlNodePtr cur) {
  10. xmlChar *uri;
  11. cur = cur-&gt;xmlChildrenNode;
  12. while (cur != NULL) {
  13. if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"reference"))) {
  14. uri = xmlGetProp(cur, "uri");
  15. printf("uri: %s\n", uri);
  16. xmlFree(uri);
  17. }
  18. cur = cur-&gt;next;
  19. }
  20. return;
  21. }
  22. void
  23. parseDoc(char *docname) {
  24. xmlDocPtr doc;
  25. xmlNodePtr cur;
  26. doc = xmlParseFile(docname);
  27. if (doc == NULL ) {
  28. fprintf(stderr,"Document not parsed successfully. \n");
  29. return;
  30. }
  31. cur = xmlDocGetRootElement(doc);
  32. if (cur == NULL) {
  33. fprintf(stderr,"empty document\n");
  34. xmlFreeDoc(doc);
  35. return;
  36. }
  37. if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
  38. fprintf(stderr,"document of the wrong type, root node != story");
  39. xmlFreeDoc(doc);
  40. return;
  41. }
  42. getReference (doc, cur);
  43. xmlFreeDoc(doc);
  44. return;
  45. }
  46. int
  47. main(int argc, char **argv) {
  48. char *docname;
  49. if (argc &lt;= 1) {
  50. printf("Usage: %s docname\n", argv[0]);
  51. return(0);
  52. }
  53. docname = argv[1];
  54. parseDoc (docname);
  55. return (1);
  56. }
  57. </pre><p>
  58. </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>