From 16ed597a93739ff63d5b26c97978b3696511a8e0 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Thu, 20 Nov 2003 18:22:31 +0000 Subject: added two xmlReader examples cleaned up some bugs in the process Daniel * doc/examples/*: added two xmlReader examples * xmlreader.c: cleaned up some bugs in the process Daniel --- doc/examples/.cvsignore | 2 + doc/examples/Makefile.am | 28 +++++++---- doc/examples/examples.xml | 116 ++++++++++++++++++++++++++++++++++++++++++++++ doc/examples/index.html | 4 +- doc/examples/index.py | 4 +- doc/examples/reader1.c | 92 ++++++++++++++++++++++++++++++++++++ doc/examples/reader1.out | 14 ++++++ doc/examples/reader2.c | 107 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 355 insertions(+), 12 deletions(-) create mode 100644 doc/examples/reader1.c create mode 100644 doc/examples/reader1.out create mode 100644 doc/examples/reader2.c (limited to 'doc/examples') diff --git a/doc/examples/.cvsignore b/doc/examples/.cvsignore index bece6eef..30fd579d 100644 --- a/doc/examples/.cvsignore +++ b/doc/examples/.cvsignore @@ -7,3 +7,5 @@ parse2 tree1 tree2 testWriter +reader1 +reader2 diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index 308bfc78..e90c867d 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -1,4 +1,4 @@ -# Beware this is autogenerated by config.py +# Beware this is autogenerated by index.py HTML_DIR=$(datadir)/doc DOC_MODULE=libxml2-$(VERSION) TARGET_DIR=$(HTML_DIR)/$(DOC_MODULE)/examples @@ -19,9 +19,9 @@ install-data-local: $(mkinstalldirs) $(DESTDIR)$(TARGET_DIR) -@INSTALL@ -m 0644 $(srcdir)/*.html $(srcdir)/*.c $(srcdir)/*.xml $(srcdir)/*.xsl $(srcdir)/*.out $(DESTDIR)$(TARGET_DIR) -EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml writer.xml tree1.out tree2.out +EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml writer.xml tree1.out tree2.out reader1.out -noinst_PROGRAMS=xpath1 parse1 parse2 tree1 tree2 testWriter +noinst_PROGRAMS=xpath1 parse1 parse2 tree1 tree2 testWriter reader1 reader2 xpath1_SOURCES=xpath1.c xpath1_LDFLAGS= @@ -53,11 +53,23 @@ testWriter_LDFLAGS= testWriter_DEPENDENCIES= $(DEPS) testWriter_LDADD= @RDL_LIBS@ $(LDADDS) +reader1_SOURCES=reader1.c +reader1_LDFLAGS= +reader1_DEPENDENCIES= $(DEPS) +reader1_LDADD= @RDL_LIBS@ $(LDADDS) + +reader2_SOURCES=reader2.c +reader2_LDFLAGS= +reader2_DEPENDENCIES= $(DEPS) +reader2_LDADD= @RDL_LIBS@ $(LDADDS) + tests: $(noinst_PROGRAMS) - parse1 test1.xml - parse2 test2.xml - tree1 test2.xml > tree1.tmp ; diff tree1.tmp tree1.out ; rm tree1.tmp - tree2 > tree2.tmp ; diff tree2.tmp tree2.out ; rm tree2.tmp - testWriter ; for i in 1 2 3 4 ; do diff writer.xml writer$$i.out ; done ; rm writer*.out + @(parse1 test1.xml) + @(parse2 test2.xml) + @(tree1 test2.xml > tree1.tmp ; diff tree1.tmp tree1.out ; rm tree1.tmp) + @(tree2 > tree2.tmp ; diff tree2.tmp tree2.out ; rm tree2.tmp) + @(testWriter ; for i in 1 2 3 4 ; do diff writer.xml writer$$i.out ; done ; rm writer*.out) + @(reader1 test2.xml > reader1.tmp ; diff reader1.tmp reader1.out ; rm reader1.tmp) + @(reader2 test2.xml > reader1.tmp ; diff reader1.tmp reader1.out ; rm reader1.tmp) diff --git a/doc/examples/examples.xml b/doc/examples/examples.xml index 4894e675..5d938bf5 100644 --- a/doc/examples/examples.xml +++ b/doc/examples/examples.xml @@ -171,6 +171,62 @@ + + Parse an XML file with an xmlReader + Demonstrate the use of xmlReaderForFile() to parse an XML file and dump the informations about the nodes found in the process + reader1 <filename> + reader1 test2.xml > reader1.tmp ; diff reader1.tmp reader1.out ; rm reader1.tmp + Daniel Veillard + see Copyright for the status of this software. +
xmlReader
+ + <libxml/xmlreader.h> + + + + + + + + + + + + + + + + +
+ + Parse and validate an XML file with an xmlReader + Demonstrate the use of xmlReaderForFile() to parse an XML file validating the content in the process and activating options like entities substitution, and DTD attributes defaulting + reader2 <valid_xml_filename> + reader2 test2.xml > reader1.tmp ; diff reader1.tmp reader1.out ; rm reader1.tmp + Daniel Veillard + see Copyright for the status of this software. +
xmlReader
+ + <libxml/xmlreader.h> + + + + + + + + + + + + + + + + + + +
@@ -178,6 +234,7 @@ + @@ -189,8 +246,15 @@ + + + + + + + @@ -210,6 +274,7 @@ + @@ -247,6 +312,10 @@ + + + + @@ -307,6 +376,10 @@ + + + + @@ -322,6 +395,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -386,6 +498,10 @@
+
+ + +
diff --git a/doc/examples/index.html b/doc/examples/index.html index 547fe148..df4f4d85 100644 --- a/doc/examples/index.html +++ b/doc/examples/index.html @@ -7,5 +7,5 @@ H1 {font-family: Verdana,Arial,Helvetica} H2 {font-family: Verdana,Arial,Helvetica} H3 {font-family: Verdana,Arial,Helvetica} A:link, A:visited, A:active { text-decoration: underline } -Libxml2 set of examples
Action against software patentsGnome2 LogoW3C LogoRed Hat Logo
Made with Libxml2 Logo

Libxml2 set of examples

Examples Menu
Related links

The examples are stored per section depending on the main focus - of the example:

Parsing Examples

parse1.c: Parse an XML file to a tree and free it

Demonstrate the use of xmlReadFile() to read an XML file into a tree and and xmlFreeDoc() to free the resulting tree

Includes:

Uses:

Usage:

parse1 test1.xml

Author: Daniel Veillard

parse2.c: Parse and validate an XML file to a tree and free the result

Create a parser context for an XML file, then parse and validate the file, creating a tree, check the validation result and xmlFreeDoc() to free the resulting tree.

Includes:

Uses:

Usage:

parse2 test2.xml

Author: Daniel Veillard

Tree Examples

tree1.c: Navigates a tree to print element names

Parse a file to a tree, use xmlDocGetRootElement() to get the root element, then walk the document and print all the element name in document order.

Includes:

Uses:

Usage:

tree1 filename_or_URL

Author: Dodji Seketeli

tree2.c: Creates a tree

Shows how to create document, nodes and dump it to stdout or file.

Includes:

Uses:

Usage:

tree2 <filename> -Default output: stdout

Author: Lucas Brasilino <brasilino@recife.pe.gov.br>

XPath Examples

xpath1.c: Evaluate XPath expression and prints result node set.

Shows how to evaluate XPath expression and register known namespaces in XPath context.

Includes:

Uses:

Usage:

xpath1 <xml-file> <xpath-expr> [<known-ns-list>]

Author: Aleksey Sanin

xmlWriter Examples

testWriter.c: use various APIs for the xmlWriter

tests a number of APIs for the xmlWriter, especially the various methods to write to a filename, to a memory buffer, to a new document, or to a subtree. It shows how to do encoding string conversions too. The resulting documents are then serialized.

Includes:

Uses:

Usage:

testWriter

Author: Alfred Mickautsch

Daniel Veillard

+Libxml2 set of examples
Action against software patentsGnome2 LogoW3C LogoRed Hat Logo
Made with Libxml2 Logo

Libxml2 set of examples

Examples Menu
Related links

The examples are stored per section depending on the main focus + of the example:

Parsing Examples

parse1.c: Parse an XML file to a tree and free it

Demonstrate the use of xmlReadFile() to read an XML file into a tree and and xmlFreeDoc() to free the resulting tree

Includes:

Uses:

Usage:

parse1 test1.xml

Author: Daniel Veillard

parse2.c: Parse and validate an XML file to a tree and free the result

Create a parser context for an XML file, then parse and validate the file, creating a tree, check the validation result and xmlFreeDoc() to free the resulting tree.

Includes:

Uses:

Usage:

parse2 test2.xml

Author: Daniel Veillard

Tree Examples

tree1.c: Navigates a tree to print element names

Parse a file to a tree, use xmlDocGetRootElement() to get the root element, then walk the document and print all the element name in document order.

Includes:

Uses:

Usage:

tree1 filename_or_URL

Author: Dodji Seketeli

tree2.c: Creates a tree

Shows how to create document, nodes and dump it to stdout or file.

Includes:

Uses:

Usage:

tree2 <filename> -Default output: stdout

Author: Lucas Brasilino <brasilino@recife.pe.gov.br>

XPath Examples

xpath1.c: Evaluate XPath expression and prints result node set.

Shows how to evaluate XPath expression and register known namespaces in XPath context.

Includes:

Uses:

Usage:

xpath1 <xml-file> <xpath-expr> [<known-ns-list>]

Author: Aleksey Sanin

xmlReader Examples

reader1.c: Parse an XML file with an xmlReader

Demonstrate the use of xmlReaderForFile() to parse an XML file and dump the informations about the nodes found in the process

Includes:

Uses:

Usage:

reader1 <filename>

Author: Daniel Veillard

reader2.c: Parse and validate an XML file with an xmlReader

Demonstrate the use of xmlReaderForFile() to parse an XML file validating the content in the process and activating options like entities substitution, and DTD attributes defaulting

Includes:

Uses:

Usage:

reader2 <valid_xml_filename>

Author: Daniel Veillard

xmlWriter Examples

testWriter.c: use various APIs for the xmlWriter

tests a number of APIs for the xmlWriter, especially the various methods to write to a filename, to a memory buffer, to a new document, or to a subtree. It shows how to do encoding string conversions too. The resulting documents are then serialized.

Includes:

Uses:

Usage:

testWriter

Author: Alfred Mickautsch

Daniel Veillard

diff --git a/doc/examples/index.py b/doc/examples/index.py index c6efd8ee..c2235b88 100755 --- a/doc/examples/index.py +++ b/doc/examples/index.py @@ -218,7 +218,7 @@ def dump_Makefile(): extras.append(file) for file in glob.glob('*.out'): extras.append(file) - Makefile="""# Beware this is autogenerated by config.py + Makefile="""# Beware this is autogenerated by index.py HTML_DIR=$(datadir)/doc DOC_MODULE=libxml2-$(VERSION) TARGET_DIR=$(HTML_DIR)/$(DOC_MODULE)/examples @@ -253,7 +253,7 @@ install-data-local: example, example) Makefile = Makefile + "tests: $(noinst_PROGRAMS)\n" for test in tests: - Makefile = Makefile + "\t%s\n" % (test) + Makefile = Makefile + "\t@(%s)\n" % (test) Makefile = Makefile + "\n\n" try: old = open("Makefile.am", "r").read() diff --git a/doc/examples/reader1.c b/doc/examples/reader1.c new file mode 100644 index 00000000..652fe977 --- /dev/null +++ b/doc/examples/reader1.c @@ -0,0 +1,92 @@ +/** + * section: xmlReader + * synopsis: Parse an XML file with an xmlReader + * purpose: Demonstrate the use of xmlReaderForFile() to parse an XML file + * and dump the informations about the nodes found in the process + * usage: reader1 + * test: reader1 test2.xml > reader1.tmp ; diff reader1.tmp reader1.out ; rm reader1.tmp + * author: Daniel Veillard + * copy: see Copyright for the status of this software. + */ + +#include +#include + +/** + * processNode: + * @reader: the xmlReader + * + * Dump information about the current node + */ +static void +processNode(xmlTextReaderPtr reader) { + const xmlChar *name, *value; + + name = xmlTextReaderConstName(reader); + if (name == NULL) + name = BAD_CAST "--"; + + value = xmlTextReaderConstValue(reader); + + printf("%d %d %s %d %d", + xmlTextReaderDepth(reader), + xmlTextReaderNodeType(reader), + name, + xmlTextReaderIsEmptyElement(reader), + xmlTextReaderHasValue(reader)); + if (value == NULL) + printf("\n"); + else { + if (xmlStrlen(value) > 40) + printf(" %.40s...\n", value); + else + printf(" %s\n", value); + } +} + +/** + * streamFile: + * @filename: the file name to parse + * + * Parse and print information about an XML file. + */ +static void +streamFile(const char *filename) { + xmlTextReaderPtr reader; + int ret; + + reader = xmlReaderForFile(filename, NULL, 0); + if (reader != NULL) { + ret = xmlTextReaderRead(reader); + while (ret == 1) { + processNode(reader); + ret = xmlTextReaderRead(reader); + } + xmlFreeTextReader(reader); + if (ret != 0) { + fprintf(stderr, "%s : failed to parse\n", filename); + } + } else { + fprintf(stderr, "Unable to open %s\n", filename); + } +} + +int main(int argc, char **argv) { + if (argc != 2) + return(1); + + /* + * this initialize the library and check potential ABI mismatches + * between the version it was compiled for and the actual shared + * library used. + */ + LIBXML_TEST_VERSION + + streamFile(argv[1]); + + /* + * Cleanup function for the XML library. + */ + xmlCleanupParser(); + return(0); +} diff --git a/doc/examples/reader1.out b/doc/examples/reader1.out new file mode 100644 index 00000000..eb48d85e --- /dev/null +++ b/doc/examples/reader1.out @@ -0,0 +1,14 @@ +0 10 doc 0 0 +0 1 doc 0 0 +1 14 #text 0 1 + +1 1 src 1 0 +1 14 #text 0 1 + +1 1 dest 1 0 +1 14 #text 0 1 + +1 1 src 1 0 +1 14 #text 0 1 + +0 15 doc 0 0 diff --git a/doc/examples/reader2.c b/doc/examples/reader2.c new file mode 100644 index 00000000..6fece7cb --- /dev/null +++ b/doc/examples/reader2.c @@ -0,0 +1,107 @@ +/** + * section: xmlReader + * synopsis: Parse and validate an XML file with an xmlReader + * purpose: Demonstrate the use of xmlReaderForFile() to parse an XML file + * validating the content in the process and activating options + * like entities substitution, and DTD attributes defaulting + * usage: reader2 + * test: reader2 test2.xml > reader1.tmp ; diff reader1.tmp reader1.out ; rm reader1.tmp + * author: Daniel Veillard + * copy: see Copyright for the status of this software. + */ + +#include +#include + +/** + * processNode: + * @reader: the xmlReader + * + * Dump information about the current node + */ +static void +processNode(xmlTextReaderPtr reader) { + const xmlChar *name, *value; + + name = xmlTextReaderConstName(reader); + if (name == NULL) + name = BAD_CAST "--"; + + value = xmlTextReaderConstValue(reader); + + printf("%d %d %s %d %d", + xmlTextReaderDepth(reader), + xmlTextReaderNodeType(reader), + name, + xmlTextReaderIsEmptyElement(reader), + xmlTextReaderHasValue(reader)); + if (value == NULL) + printf("\n"); + else { + if (xmlStrlen(value) > 40) + printf(" %.40s...\n", value); + else + printf(" %s\n", value); + } +} + +/** + * streamFile: + * @filename: the file name to parse + * + * Parse, validate and print information about an XML file. + */ +static void +streamFile(const char *filename) { + xmlTextReaderPtr reader; + int ret; + + + /* + * Pass some special parsing options to activate DTD attribute defaulting, + * entities substitution and DTD validation + */ + reader = xmlReaderForFile(filename, NULL, + XML_PARSE_DTDATTR | /* default DTD attributes */ + XML_PARSE_NOENT | /* substitute entities */ + XML_PARSE_DTDVALID); /* validate with the DTD */ + if (reader != NULL) { + ret = xmlTextReaderRead(reader); + while (ret == 1) { + processNode(reader); + ret = xmlTextReaderRead(reader); + } + /* + * Once the document has been fully parsed check the validation results + */ + if (xmlTextReaderIsValid(reader) != 1) { + fprintf(stderr, "Document %s does not validate\n", filename); + } + xmlFreeTextReader(reader); + if (ret != 0) { + fprintf(stderr, "%s : failed to parse\n", filename); + } + } else { + fprintf(stderr, "Unable to open %s\n", filename); + } +} + +int main(int argc, char **argv) { + if (argc != 2) + return(1); + + /* + * this initialize the library and check potential ABI mismatches + * between the version it was compiled for and the actual shared + * library used. + */ + LIBXML_TEST_VERSION + + streamFile(argv[1]); + + /* + * Cleanup function for the XML library. + */ + xmlCleanupParser(); + return(0); +} -- cgit v1.2.1