includekeyword.c 1.3 KB

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