reader4.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * section: xmlReader
  3. * synopsis: Parse multiple XML files reusing an xmlReader
  4. * purpose: Demonstrate the use of xmlReaderForFile() and
  5. * xmlReaderNewFile to parse XML files while reusing the reader object
  6. * and parser context. (Note that the XMLReader functions require
  7. * libxml2 version later than 2.6.)
  8. * usage: reader4 <filename> [ filename ... ]
  9. * test: reader4 test1.xml test2.xml test3.xml > reader4.tmp ; diff reader4.tmp reader4.res ; rm reader4.tmp
  10. * author: Graham Bennett
  11. * copy: see Copyright for the status of this software.
  12. */
  13. #include <stdio.h>
  14. #include <libxml/xmlreader.h>
  15. #ifdef LIBXML_READER_ENABLED
  16. static void processDoc(xmlTextReaderPtr readerPtr) {
  17. int ret;
  18. xmlDocPtr docPtr;
  19. const xmlChar *URL;
  20. ret = xmlTextReaderRead(readerPtr);
  21. while (ret == 1) {
  22. ret = xmlTextReaderRead(readerPtr);
  23. }
  24. /*
  25. * One can obtain the document pointer to get insteresting
  26. * information about the document like the URL, but one must also
  27. * be sure to clean it up at the end (see below).
  28. */
  29. docPtr = xmlTextReaderCurrentDoc(readerPtr);
  30. if (NULL == docPtr) {
  31. fprintf(stderr, "failed to obtain document\n");
  32. return;
  33. }
  34. URL = docPtr->URL;
  35. if (NULL == URL) {
  36. fprintf(stderr, "Failed to obtain URL\n");
  37. }
  38. if (ret != 0) {
  39. fprintf(stderr, "%s: Failed to parse\n", URL);
  40. return;
  41. }
  42. printf("%s: Processed ok\n", (const char *)URL);
  43. }
  44. int main(int argc, char **argv) {
  45. xmlTextReaderPtr readerPtr;
  46. int i;
  47. xmlDocPtr docPtr;
  48. if (argc < 2)
  49. return(1);
  50. /*
  51. * this initialises the library and check potential ABI mismatches
  52. * between the version it was compiled for and the actual shared
  53. * library used.
  54. */
  55. LIBXML_TEST_VERSION
  56. /*
  57. * Create a new reader for the first file and process the
  58. * document.
  59. */
  60. readerPtr = xmlReaderForFile(argv[1], NULL, 0);
  61. if (NULL == readerPtr) {
  62. fprintf(stderr, "%s: failed to create reader\n", argv[1]);
  63. return(1);
  64. }
  65. processDoc(readerPtr);
  66. /*
  67. * The reader can be reused for subsequent files.
  68. */
  69. for (i=2; i < argc; ++i) {
  70. xmlReaderNewFile(readerPtr, argv[i], NULL, 0);
  71. if (NULL == readerPtr) {
  72. fprintf(stderr, "%s: failed to create reader\n", argv[i]);
  73. return(1);
  74. }
  75. processDoc(readerPtr);
  76. }
  77. /*
  78. * Since we've called xmlTextReaderCurrentDoc, we now have to
  79. * clean up after ourselves. We only have to do this the last
  80. * time, because xmlReaderNewFile calls xmlCtxtReset which takes
  81. * care of it.
  82. */
  83. docPtr = xmlTextReaderCurrentDoc(readerPtr);
  84. if (docPtr != NULL)
  85. xmlFreeDoc(docPtr);
  86. /*
  87. * Clean up the reader.
  88. */
  89. xmlFreeTextReader(readerPtr);
  90. /*
  91. * Cleanup function for the XML library.
  92. */
  93. xmlCleanupParser();
  94. /*
  95. * this is to debug memory for regression tests
  96. */
  97. xmlMemoryDump();
  98. return(0);
  99. }
  100. #else
  101. int main(void) {
  102. fprintf(stderr, "xmlReader support not compiled in\n");
  103. exit(1);
  104. }
  105. #endif