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