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 $(srcdir)/reader4.res
10 * author: Graham Bennett
11 * copy: see Copyright for the status of this software.
15 #include <libxml/xmlreader.h>
17 #ifdef LIBXML_READER_ENABLED
19 static void processDoc(xmlTextReaderPtr readerPtr) {
24 ret = xmlTextReaderRead(readerPtr);
26 ret = xmlTextReaderRead(readerPtr);
30 * One can obtain the document pointer to get insteresting
31 * information about the document like the URL, but one must also
32 * be sure to clean it up at the end (see below).
34 docPtr = xmlTextReaderCurrentDoc(readerPtr);
36 fprintf(stderr, "failed to obtain document\n");
42 fprintf(stderr, "Failed to obtain URL\n");
46 fprintf(stderr, "%s: Failed to parse\n", URL);
50 printf("%s: Processed ok\n", (const char *)URL);
53 int main(int argc, char **argv) {
54 xmlTextReaderPtr readerPtr;
62 * this initialises the library and check potential ABI mismatches
63 * between the version it was compiled for and the actual shared
69 * Create a new reader for the first file and process the
72 readerPtr = xmlReaderForFile(argv[1], NULL, 0);
73 if (NULL == readerPtr) {
74 fprintf(stderr, "%s: failed to create reader\n", argv[1]);
77 processDoc(readerPtr);
80 * The reader can be reused for subsequent files.
82 for (i=2; i < argc; ++i) {
83 xmlReaderNewFile(readerPtr, argv[i], NULL, 0);
84 if (NULL == readerPtr) {
85 fprintf(stderr, "%s: failed to create reader\n", argv[i]);
88 processDoc(readerPtr);
92 * Since we've called xmlTextReaderCurrentDoc, we now have to
93 * clean up after ourselves. We only have to do this the last
94 * time, because xmlReaderNewFile calls xmlCtxtReset which takes
97 docPtr = xmlTextReaderCurrentDoc(readerPtr);
102 * Clean up the reader.
104 xmlFreeTextReader(readerPtr);
107 * Cleanup function for the XML library.
111 * this is to debug memory for regression tests
119 fprintf(stderr, "xmlReader support not compiled in\n");