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
9 * usage: reader2 <valid_xml_filename>
10 * test: reader2 test2.xml > reader1.tmp ; diff reader1.tmp reader1.res ; rm reader1.tmp
11 * author: Daniel Veillard
12 * copy: see Copyright for the status of this software.
16 #include <libxml/xmlreader.h>
18 #ifdef LIBXML_READER_ENABLED
22 * @reader: the xmlReader
24 * Dump information about the current node
27 processNode(xmlTextReaderPtr reader) {
28 const xmlChar *name, *value;
30 name = xmlTextReaderConstName(reader);
34 value = xmlTextReaderConstValue(reader);
36 printf("%d %d %s %d %d",
37 xmlTextReaderDepth(reader),
38 xmlTextReaderNodeType(reader),
40 xmlTextReaderIsEmptyElement(reader),
41 xmlTextReaderHasValue(reader));
45 if (xmlStrlen(value) > 40)
46 printf(" %.40s...\n", value);
48 printf(" %s\n", value);
54 * @filename: the file name to parse
56 * Parse, validate and print information about an XML file.
59 streamFile(const char *filename) {
60 xmlTextReaderPtr reader;
65 * Pass some special parsing options to activate DTD attribute defaulting,
66 * entities substitution and DTD validation
68 reader = xmlReaderForFile(filename, NULL,
69 XML_PARSE_DTDATTR /* default DTD attributes */
70 | XML_PARSE_NOENT /* substitute entities */
73 | XML_PARSE_DTDVALID /* validate with the DTD */
77 ret = xmlTextReaderRead(reader);
80 ret = xmlTextReaderRead(reader);
83 * Once the document has been fully parsed check the validation results
85 if (xmlTextReaderIsValid(reader) != 1) {
86 fprintf(stderr, "Document %s does not validate\n", filename);
88 xmlFreeTextReader(reader);
90 fprintf(stderr, "%s : failed to parse\n", filename);
93 fprintf(stderr, "Unable to open %s\n", filename);
97 int main(int argc, char **argv) {
102 * this initialize the library and check potential ABI mismatches
103 * between the version it was compiled for and the actual shared
111 * Cleanup function for the XML library.
115 * this is to debug memory for regression tests
123 fprintf(stderr, "XInclude support not compiled in\n");