includeaddkeyword.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <![CDATA[
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <libxml/xmlmemory.h>
  6. #include <libxml/parser.h>
  7. void
  8. parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
  9. xmlNewTextChild (cur, NULL, "keyword", keyword);
  10. return;
  11. }
  12. xmlDocPtr
  13. parseDoc(char *docname, char *keyword) {
  14. xmlDocPtr doc;
  15. xmlNodePtr cur;
  16. doc = xmlParseFile(docname);
  17. if (doc == NULL ) {
  18. fprintf(stderr,"Document not parsed successfully. \n");
  19. return (NULL);
  20. }
  21. cur = xmlDocGetRootElement(doc);
  22. if (cur == NULL) {
  23. fprintf(stderr,"empty document\n");
  24. xmlFreeDoc(doc);
  25. return (NULL);
  26. }
  27. if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
  28. fprintf(stderr,"document of the wrong type, root node != story");
  29. xmlFreeDoc(doc);
  30. return (NULL);
  31. }
  32. cur = cur->xmlChildrenNode;
  33. while (cur != NULL) {
  34. if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
  35. parseStory (doc, cur, keyword);
  36. }
  37. cur = cur->next;
  38. }
  39. return(doc);
  40. }
  41. int
  42. main(int argc, char **argv) {
  43. char *docname;
  44. char *keyword;
  45. xmlDocPtr doc;
  46. if (argc <= 2) {
  47. printf("Usage: %s docname, keyword\n", argv[0]);
  48. return(0);
  49. }
  50. docname = argv[1];
  51. keyword = argv[2];
  52. doc = parseDoc (docname, keyword);
  53. if (doc != NULL) {
  54. xmlSaveFormatFile (docname, doc, 0);
  55. xmlFreeDoc(doc);
  56. }
  57. return (1);
  58. }
  59. ]]>