includegetattribute.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. getReference (xmlDocPtr doc, xmlNodePtr cur) {
  9. xmlChar *uri;
  10. cur = cur->xmlChildrenNode;
  11. while (cur != NULL) {
  12. if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
  13. uri = xmlGetProp(cur, "uri");
  14. printf("uri: %s\n", uri);
  15. xmlFree(uri);
  16. }
  17. cur = cur->next;
  18. }
  19. return;
  20. }
  21. 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. getReference (doc, cur);
  42. xmlFreeDoc(doc);
  43. return;
  44. }
  45. int
  46. main(int argc, char **argv) {
  47. char *docname;
  48. if (argc <= 1) {
  49. printf("Usage: %s docname\n", argv[0]);
  50. return(0);
  51. }
  52. docname = argv[1];
  53. parseDoc (docname);
  54. return (1);
  55. }
  56. ]]>