VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.31/doc/examples/reader3.c@ 44536

Last change on this file since 44536 was 39915, checked in by vboxsync, 13 years ago

libxml-2.6.31 unmodified

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette