apf.html 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>F. Code for Add Attribute 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="ape.html" title="E. Code for Add Keyword Example"><link rel="next" href="apg.html" title="G. Code for Retrieving Attribute Value 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">F. Code for Add Attribute Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ape.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="apg.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="addattributeappendix"></a>F. Code for Add Attribute 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. xmlDocPtr
  9. parseDoc(char *docname, char *uri) {
  10. xmlDocPtr doc;
  11. xmlNodePtr cur;
  12. xmlNodePtr newnode;
  13. xmlAttrPtr newattr;
  14. doc = xmlParseFile(docname);
  15. if (doc == NULL ) {
  16. fprintf(stderr,"Document not parsed successfully. \n");
  17. return (NULL);
  18. }
  19. cur = xmlDocGetRootElement(doc);
  20. if (cur == NULL) {
  21. fprintf(stderr,"empty document\n");
  22. xmlFreeDoc(doc);
  23. return (NULL);
  24. }
  25. if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
  26. fprintf(stderr,"document of the wrong type, root node != story");
  27. xmlFreeDoc(doc);
  28. return (NULL);
  29. }
  30. newnode = xmlNewTextChild (cur, NULL, "reference", NULL);
  31. newattr = xmlNewProp (newnode, "uri", uri);
  32. return(doc);
  33. }
  34. int
  35. main(int argc, char **argv) {
  36. char *docname;
  37. char *uri;
  38. xmlDocPtr doc;
  39. if (argc &lt;= 2) {
  40. printf("Usage: %s docname, uri\n", argv[0]);
  41. return(0);
  42. }
  43. docname = argv[1];
  44. uri = argv[2];
  45. doc = parseDoc (docname, uri);
  46. if (doc != NULL) {
  47. xmlSaveFormatFile (docname, doc, 1);
  48. xmlFreeDoc(doc);
  49. }
  50. return (1);
  51. }
  52. </pre><p>
  53. </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ape.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="apg.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">E. Code for Add Keyword Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> G. Code for Retrieving Attribute Value Example</td></tr></table></div></body></html>