reader3.c revision f2497c16b223b8be56d0496fc0bce84ed2ce3b6e
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
15#include <stdio.h>
16#include <libxml/xmlreader.h>
17
18/**
19 * streamFile:
20 * @filename: the file name to parse
21 *
22 * Parse and print information about an XML file.
23 *
24 * Returns the resulting doc with just the elements preserved.
25 */
26static xmlDocPtr
27extractFile(const char *filename, const xmlChar *pattern) {
28    xmlDocPtr doc;
29    xmlTextReaderPtr reader;
30    int ret;
31
32    /*
33     * build an xmlReader for that file
34     */
35    reader = xmlReaderForFile(filename, NULL, 0);
36    if (reader != NULL) {
37        /*
38	 * add the pattern to preserve
39	 */
40        if (xmlTextReaderPreservePattern(reader, pattern, NULL) < 0) {
41            fprintf(stderr, "%s : failed add preserve pattern %s\n",
42	            filename, (const char *) pattern);
43	}
44	/*
45	 * Parse and traverse the tree, collecting the nodes in the process
46	 */
47        ret = xmlTextReaderRead(reader);
48        while (ret == 1) {
49            ret = xmlTextReaderRead(reader);
50        }
51        if (ret != 0) {
52            fprintf(stderr, "%s : failed to parse\n", filename);
53	    xmlFreeTextReader(reader);
54	    return(NULL);
55        }
56	/*
57	 * get the resulting nodes
58	 */
59	doc = xmlTextReaderCurrentDoc(reader);
60	/*
61	 * Free up the reader
62	 */
63        xmlFreeTextReader(reader);
64    } else {
65        fprintf(stderr, "Unable to open %s\n", filename);
66	return(NULL);
67    }
68    return(doc);
69}
70
71int main(int argc, char **argv) {
72    const char *filename = "test3.xml";
73    const char *pattern = "preserved";
74    xmlDocPtr doc;
75
76    if (argc == 3) {
77        filename = argv[1];
78	pattern = argv[2];
79    }
80
81    /*
82     * this initialize the library and check potential ABI mismatches
83     * between the version it was compiled for and the actual shared
84     * library used.
85     */
86    LIBXML_TEST_VERSION
87
88    doc = extractFile(filename, (const xmlChar *) pattern);
89    if (doc != NULL) {
90        /*
91	 * ouptut the result.
92	 */
93        xmlDocDump(stdout, doc);
94	/*
95	 * don't forget to free up the doc
96	 */
97	xmlFreeDoc(doc);
98    }
99
100
101    /*
102     * Cleanup function for the XML library.
103     */
104    xmlCleanupParser();
105    /*
106     * this is to debug memory for regression tests
107     */
108    xmlMemoryDump();
109    return(0);
110}
111