1 /**
2 * section: xmlReader
3 * synopsis: Parse and validate an XML file with an xmlReader
4 * purpose: Demonstrate the use of xmlReaderForFile() to parse an XML file
5 * validating the content in the process and activating options
6 * like entities substitution, and DTD attributes defaulting.
7 * (Note that the XMLReader functions require libxml2 version later
8 * than 2.6.)
9 * usage: reader2 <valid_xml_filename>
10 * test: reader2 test2.xml > reader1.tmp && diff reader1.tmp $(srcdir)/reader1.res
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 #ifdef LIBXML_READER_ENABLED
19
20 /**
21 * processNode:
22 * @reader: the xmlReader
23 *
24 * Dump information about the current node
25 */
26 static void
processNode(xmlTextReaderPtr reader)27 processNode(xmlTextReaderPtr reader) {
28 const xmlChar *name, *value;
29
30 name = xmlTextReaderConstName(reader);
31 if (name == NULL)
32 name = BAD_CAST "--";
33
34 value = xmlTextReaderConstValue(reader);
35
36 printf("%d %d %s %d %d",
37 xmlTextReaderDepth(reader),
38 xmlTextReaderNodeType(reader),
39 name,
40 xmlTextReaderIsEmptyElement(reader),
41 xmlTextReaderHasValue(reader));
42 if (value == NULL)
43 printf("\n");
44 else {
45 if (xmlStrlen(value) > 40)
46 printf(" %.40s...\n", value);
47 else
48 printf(" %s\n", value);
49 }
50 }
51
52 /**
53 * streamFile:
54 * @filename: the file name to parse
55 *
56 * Parse, validate and print information about an XML file.
57 */
58 static void
streamFile(const char * filename)59 streamFile(const char *filename) {
60 xmlTextReaderPtr reader;
61 int ret;
62
63
64 /*
65 * Pass some special parsing options to activate DTD attribute defaulting,
66 * entities substitution and DTD validation
67 */
68 reader = xmlReaderForFile(filename, NULL,
69 XML_PARSE_DTDATTR | /* default DTD attributes */
70 XML_PARSE_NOENT | /* substitute entities */
71 XML_PARSE_DTDVALID); /* validate with the DTD */
72 if (reader != NULL) {
73 ret = xmlTextReaderRead(reader);
74 while (ret == 1) {
75 processNode(reader);
76 ret = xmlTextReaderRead(reader);
77 }
78 /*
79 * Once the document has been fully parsed check the validation results
80 */
81 if (xmlTextReaderIsValid(reader) != 1) {
82 fprintf(stderr, "Document %s does not validate\n", filename);
83 }
84 xmlFreeTextReader(reader);
85 if (ret != 0) {
86 fprintf(stderr, "%s : failed to parse\n", filename);
87 }
88 } else {
89 fprintf(stderr, "Unable to open %s\n", filename);
90 }
91 }
92
main(int argc,char ** argv)93 int main(int argc, char **argv) {
94 if (argc != 2)
95 return(1);
96
97 /*
98 * this initialize the library and check potential ABI mismatches
99 * between the version it was compiled for and the actual shared
100 * library used.
101 */
102 LIBXML_TEST_VERSION
103
104 streamFile(argv[1]);
105
106 /*
107 * Cleanup function for the XML library.
108 */
109 xmlCleanupParser();
110 /*
111 * this is to debug memory for regression tests
112 */
113 xmlMemoryDump();
114 return(0);
115 }
116
117 #else
main(void)118 int main(void) {
119 fprintf(stderr, "XInclude support not compiled in\n");
120 return(0);
121 }
122 #endif
123