From d9d63d6e53cdc0a5420c3eccbdbc99c236168d7f Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Thu, 13 Nov 2003 11:45:43 +0000 Subject: [PATCH] added Dodji's example, added output handling Daniel * doc/examples/*: added Dodji's example, added output handling Daniel --- ChangeLog | 4 ++ doc/examples/.cvsignore | 1 + doc/examples/Makefile.am | 10 ++++- doc/examples/examples.xml | 32 +++++++++++++++ doc/examples/index.html | 4 +- doc/examples/index.py | 2 + doc/examples/tree1.c | 86 +++++++++++++++++++++++++++++++++++++++ doc/examples/tree1.out | 4 ++ 8 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 doc/examples/tree1.c create mode 100644 doc/examples/tree1.out diff --git a/ChangeLog b/ChangeLog index 22557599..6372eefd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Nov 13 12:44:14 CET 2003 Daniel Veillard + + * doc/examples/*: added Dodji's example, added output handling + Thu Nov 13 11:35:35 CET 2003 Daniel Veillard * doc/examples/*: added Aleksey XPath example, fixed bugs diff --git a/doc/examples/.cvsignore b/doc/examples/.cvsignore index 1b8c0a1a..aa4aa989 100644 --- a/doc/examples/.cvsignore +++ b/doc/examples/.cvsignore @@ -4,3 +4,4 @@ Makefile xpath1 parse1 parse2 +tree1 diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index 0d24a67a..9fa19a3f 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -11,9 +11,9 @@ examples.xml: index.py *.c index.html: examples.xml examples.xsl -@(xsltproc examples.xsl examples.xml && echo "Rebuilt web page" && xmllint --valid --noout index.html) -EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml +EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml tree1.out -noinst_PROGRAMS=xpath1 parse1 parse2 +noinst_PROGRAMS=xpath1 parse1 parse2 tree1 xpath1_SOURCES=xpath1.c xpath1_LDFLAGS= @@ -30,8 +30,14 @@ parse2_LDFLAGS= parse2_DEPENDENCIES= $(DEPS) parse2_LDADD= @RDL_LIBS@ $(LDADDS) +tree1_SOURCES=tree1.c +tree1_LDFLAGS= +tree1_DEPENDENCIES= $(DEPS) +tree1_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 diff --git a/doc/examples/examples.xml b/doc/examples/examples.xml index c094cd70..ec8b438c 100644 --- a/doc/examples/examples.xml +++ b/doc/examples/examples.xml @@ -78,14 +78,37 @@ + + 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. + tree1 filename_or_URL + tree1 test2.xml > tree1.tmp ; diff tree1.tmp tree1.out ; rm tree1.tmp + Dodji Seketeli + see Copyright for the status of this software. +
Tree
+ + <libxml/tree.h> + <libxml/parser.h> + + + + + + + + + +
+ + @@ -97,10 +120,14 @@ + + + + @@ -110,6 +137,7 @@ + @@ -128,6 +156,7 @@ + @@ -168,6 +197,9 @@ +
+ +
diff --git a/doc/examples/index.html b/doc/examples/index.html index aa691276..aef3447e 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

Main Menu
Related links

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

  • XPath :

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

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

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

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

Daniel Veillard

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

Libxml2 set of examples

Main Menu
Related links

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

  • Tree :

    • tree1.c: Navigates a tree to print element names
  • XPath :

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

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

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

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

Daniel Veillard

diff --git a/doc/examples/index.py b/doc/examples/index.py index 3fbc246a..bbd0e8ce 100755 --- a/doc/examples/index.py +++ b/doc/examples/index.py @@ -216,6 +216,8 @@ def dump_sections(output): def dump_Makefile(): for file in glob.glob('*.xml'): extras.append(file) + for file in glob.glob('*.out'): + extras.append(file) Makefile="""# Beware this is autogenerated by config.py INCLUDES = -I$(top_builddir)/include -I@srcdir@/include @THREAD_CFLAGS@ @Z_CFLAGS@ DEPS = $(top_builddir)/libxml2.la diff --git a/doc/examples/tree1.c b/doc/examples/tree1.c new file mode 100644 index 00000000..bd201996 --- /dev/null +++ b/doc/examples/tree1.c @@ -0,0 +1,86 @@ +/** + * section: Tree + * synopsis: Navigates a tree to print element names + * purpose: 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. + * usage: tree1 filename_or_URL + * test: tree1 test2.xml > tree1.tmp ; diff tree1.tmp tree1.out ; rm tree1.tmp + * author: Dodji Seketeli + * copy: see Copyright for the status of this software. + */ +#include +#include +#include + +/* + *To compile this file using gcc you can type + *gcc `xml2-config --cflags --libs` -o xmlexample libxml2-example.c + */ + +/** + * print_element_names: + * @a_node: the initial xml node to consider. + * + * Prints the names of the all the xml elements + * that are siblings or children of a given xml node. + */ +void +print_element_names(xmlNode * a_node) +{ + xmlNode *cur_node = NULL; + + for (cur_node = a_node; cur_node; cur_node = cur_node->next) { + if (cur_node->type == XML_ELEMENT_NODE) { + printf("node type: Element, name: %s\n", cur_node->name); + } + + print_element_names(cur_node->children); + } +} + + +/** + * Simple example to parse a file called "file.xml", + * walk down the DOM, and print the name of the + * xml elements nodes. + */ +int +main(int argc, char **argv) +{ + xmlDoc *doc = NULL; + xmlNode *root_element = NULL; + + 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 + + /*parse the file and get the DOM */ + doc = xmlParseFile(argv[1]); + + if (doc == NULL) { + printf("error: could not parse file file.xml\n"); + } + + /*Get the root element node */ + root_element = xmlDocGetRootElement(doc); + + print_element_names(root_element); + + /*free the document */ + xmlFreeDoc(doc); + + /* + *Free the global variables that may + *have been allocated by the parser. + */ + xmlCleanupParser(); + + return 0; +} diff --git a/doc/examples/tree1.out b/doc/examples/tree1.out new file mode 100644 index 00000000..7b14ad0c --- /dev/null +++ b/doc/examples/tree1.out @@ -0,0 +1,4 @@ +node type: Element, name: doc +node type: Element, name: src +node type: Element, name: dest +node type: Element, name: src