reader3.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * section: xmlReader
  3. * synopsis: Show how to extract subdocuments with xmlReader
  4. * purpose: Demonstrate the use of xmlTextReaderPreservePattern()
  5. * to parse an XML file with the xmlReader while collecting
  6. * only some subparts of the document.
  7. * (Note that the XMLReader functions require libxml2 version later
  8. * than 2.6.)
  9. * usage: reader3
  10. * test: reader3 > reader3.tmp ; diff reader3.tmp reader3.res ; rm reader3.tmp
  11. * author: Daniel Veillard
  12. * copy: see Copyright for the status of this software.
  13. */
  14. #include <stdio.h>
  15. #include <libxml/xmlreader.h>
  16. #if defined(LIBXML_READER_ENABLED) && defined(LIBXML_PATTERN_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
  17. /**
  18. * streamFile:
  19. * @filename: the file name to parse
  20. *
  21. * Parse and print information about an XML file.
  22. *
  23. * Returns the resulting doc with just the elements preserved.
  24. */
  25. static xmlDocPtr
  26. extractFile(const char *filename, const xmlChar *pattern) {
  27. xmlDocPtr doc;
  28. xmlTextReaderPtr reader;
  29. int ret;
  30. /*
  31. * build an xmlReader for that file
  32. */
  33. reader = xmlReaderForFile(filename, NULL, 0);
  34. if (reader != NULL) {
  35. /*
  36. * add the pattern to preserve
  37. */
  38. if (xmlTextReaderPreservePattern(reader, pattern, NULL) < 0) {
  39. fprintf(stderr, "%s : failed add preserve pattern %s\n",
  40. filename, (const char *) pattern);
  41. }
  42. /*
  43. * Parse and traverse the tree, collecting the nodes in the process
  44. */
  45. ret = xmlTextReaderRead(reader);
  46. while (ret == 1) {
  47. ret = xmlTextReaderRead(reader);
  48. }
  49. if (ret != 0) {
  50. fprintf(stderr, "%s : failed to parse\n", filename);
  51. xmlFreeTextReader(reader);
  52. return(NULL);
  53. }
  54. /*
  55. * get the resulting nodes
  56. */
  57. doc = xmlTextReaderCurrentDoc(reader);
  58. /*
  59. * Free up the reader
  60. */
  61. xmlFreeTextReader(reader);
  62. } else {
  63. fprintf(stderr, "Unable to open %s\n", filename);
  64. return(NULL);
  65. }
  66. return(doc);
  67. }
  68. int main(int argc, char **argv) {
  69. const char *filename = "test3.xml";
  70. const char *pattern = "preserved";
  71. xmlDocPtr doc;
  72. if (argc == 3) {
  73. filename = argv[1];
  74. pattern = argv[2];
  75. }
  76. /*
  77. * this initialize the library and check potential ABI mismatches
  78. * between the version it was compiled for and the actual shared
  79. * library used.
  80. */
  81. LIBXML_TEST_VERSION
  82. doc = extractFile(filename, (const xmlChar *) pattern);
  83. if (doc != NULL) {
  84. /*
  85. * ouptut the result.
  86. */
  87. xmlDocDump(stdout, doc);
  88. /*
  89. * don't forget to free up the doc
  90. */
  91. xmlFreeDoc(doc);
  92. }
  93. /*
  94. * Cleanup function for the XML library.
  95. */
  96. xmlCleanupParser();
  97. /*
  98. * this is to debug memory for regression tests
  99. */
  100. xmlMemoryDump();
  101. return(0);
  102. }
  103. #else
  104. int main(void) {
  105. fprintf(stderr, "Reader, Pattern or output support not compiled in\n");
  106. exit(1);
  107. }
  108. #endif