libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / doc / examples / reader4.c
blob1289d0d4f471b06a96a2ebba130044cb7770c94d
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 $(srcdir)/reader4.res
10 * author: Graham Bennett
11 * copy: see Copyright for the status of this software.
14 #include <stdio.h>
15 #include <libxml/xmlreader.h>
17 #ifdef LIBXML_READER_ENABLED
19 static void processDoc(xmlTextReaderPtr readerPtr) {
20 int ret;
21 xmlDocPtr docPtr;
22 const xmlChar *URL;
24 ret = xmlTextReaderRead(readerPtr);
25 while (ret == 1) {
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);
35 if (NULL == docPtr) {
36 fprintf(stderr, "failed to obtain document\n");
37 return;
40 URL = docPtr->URL;
41 if (NULL == URL) {
42 fprintf(stderr, "Failed to obtain URL\n");
45 if (ret != 0) {
46 fprintf(stderr, "%s: Failed to parse\n", URL);
47 return;
50 printf("%s: Processed ok\n", (const char *)URL);
53 int main(int argc, char **argv) {
54 xmlTextReaderPtr readerPtr;
55 int i;
56 xmlDocPtr docPtr;
58 if (argc < 2)
59 return(1);
62 * this initialises the library and check potential ABI mismatches
63 * between the version it was compiled for and the actual shared
64 * library used.
66 LIBXML_TEST_VERSION
69 * Create a new reader for the first file and process the
70 * document.
72 readerPtr = xmlReaderForFile(argv[1], NULL, 0);
73 if (NULL == readerPtr) {
74 fprintf(stderr, "%s: failed to create reader\n", argv[1]);
75 return(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]);
86 return(1);
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
95 * care of it.
97 docPtr = xmlTextReaderCurrentDoc(readerPtr);
98 if (docPtr != NULL)
99 xmlFreeDoc(docPtr);
102 * Clean up the reader.
104 xmlFreeTextReader(readerPtr);
107 * Cleanup function for the XML library.
109 xmlCleanupParser();
111 * this is to debug memory for regression tests
113 xmlMemoryDump();
114 return(0);
117 #else
118 int main(void) {
119 fprintf(stderr, "xmlReader support not compiled in\n");
120 exit(1);
122 #endif