ape.html 3.0 KB

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