diff options
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/Makefile.am | 11 | ||||
-rw-r--r-- | doc/examples/Makefile.in | 34 | ||||
-rw-r--r-- | doc/examples/examples.xml | 29 | ||||
-rwxr-xr-x | doc/examples/index.py | 2 | ||||
-rw-r--r-- | doc/examples/reader4.c | 122 | ||||
-rw-r--r-- | doc/examples/reader4.res | 3 |
6 files changed, 188 insertions, 13 deletions
diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index 26d143f..9667916 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -15,9 +15,9 @@ install-data-local: $(mkinstalldirs) $(DESTDIR)$(HTML_DIR) -@INSTALL@ -m 0644 $(srcdir)/*.html $(srcdir)/*.c $(srcdir)/*.xml $(srcdir)/*.xsl $(srcdir)/*.res $(DESTDIR)$(HTML_DIR) -EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml writer.xml test3.xml tst.xml reader1.res reader3.res tree1.res tree2.res io1.res io2.res xpath1.res xpath2.res +EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml writer.xml test3.xml tst.xml reader1.res reader3.res tree1.res tree2.res io1.res io2.res xpath1.res xpath2.res reader4.res -noinst_PROGRAMS=xpath1 parse1 parse2 tree1 tree2 testWriter reader1 reader2 reader3 io1 parse3 parse4 io2 xpath2 +noinst_PROGRAMS=xpath1 parse1 parse2 tree1 tree2 testWriter reader1 reader2 reader3 io1 parse3 parse4 io2 xpath2 reader4 xpath1_SOURCES=xpath1.c xpath1_LDFLAGS= @@ -89,6 +89,11 @@ xpath2_LDFLAGS= xpath2_DEPENDENCIES= $(DEPS) xpath2_LDADD= @RDL_LIBS@ $(LDADDS) +reader4_SOURCES=reader4.c +reader4_LDFLAGS= +reader4_DEPENDENCIES= $(DEPS) +reader4_LDADD= @RDL_LIBS@ $(LDADDS) + valgrind: $(MAKE) CHECKER='valgrind -q' tests @@ -123,5 +128,7 @@ tests: $(noinst_PROGRAMS) @(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0) @($(CHECKER) ./xpath2 test3.xml '//discarded' discarded > xpath2.tmp ; diff xpath2.tmp xpath2.res ; rm xpath2.tmp) @(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0) + @($(CHECKER) ./reader4 test1.xml test2.xml test3.xml > reader4.tmp ; diff reader4.tmp reader4.res ; rm reader4.tmp) + @(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0) diff --git a/doc/examples/Makefile.in b/doc/examples/Makefile.in index 216122d..838ce14 100644 --- a/doc/examples/Makefile.in +++ b/doc/examples/Makefile.in @@ -14,7 +14,7 @@ @SET_MAKE@ -SOURCES = $(io1_SOURCES) $(io2_SOURCES) $(parse1_SOURCES) $(parse2_SOURCES) $(parse3_SOURCES) $(parse4_SOURCES) $(reader1_SOURCES) $(reader2_SOURCES) $(reader3_SOURCES) $(testWriter_SOURCES) $(tree1_SOURCES) $(tree2_SOURCES) $(xpath1_SOURCES) $(xpath2_SOURCES) +SOURCES = $(io1_SOURCES) $(io2_SOURCES) $(parse1_SOURCES) $(parse2_SOURCES) $(parse3_SOURCES) $(parse4_SOURCES) $(reader1_SOURCES) $(reader2_SOURCES) $(reader3_SOURCES) $(reader4_SOURCES) $(testWriter_SOURCES) $(tree1_SOURCES) $(tree2_SOURCES) $(xpath1_SOURCES) $(xpath2_SOURCES) srcdir = @srcdir@ top_srcdir = @top_srcdir@ @@ -41,7 +41,7 @@ noinst_PROGRAMS = xpath1$(EXEEXT) parse1$(EXEEXT) parse2$(EXEEXT) \ tree1$(EXEEXT) tree2$(EXEEXT) testWriter$(EXEEXT) \ reader1$(EXEEXT) reader2$(EXEEXT) reader3$(EXEEXT) \ io1$(EXEEXT) parse3$(EXEEXT) parse4$(EXEEXT) io2$(EXEEXT) \ - xpath2$(EXEEXT) + xpath2$(EXEEXT) reader4$(EXEEXT) subdir = doc/examples DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -73,6 +73,8 @@ am_reader2_OBJECTS = reader2.$(OBJEXT) reader2_OBJECTS = $(am_reader2_OBJECTS) am_reader3_OBJECTS = reader3.$(OBJEXT) reader3_OBJECTS = $(am_reader3_OBJECTS) +am_reader4_OBJECTS = reader4.$(OBJEXT) +reader4_OBJECTS = $(am_reader4_OBJECTS) am_testWriter_OBJECTS = testWriter.$(OBJEXT) testWriter_OBJECTS = $(am_testWriter_OBJECTS) am_tree1_OBJECTS = tree1.$(OBJEXT) @@ -90,9 +92,10 @@ am__depfiles_maybe = depfiles @AMDEP_TRUE@ ./$(DEPDIR)/parse1.Po ./$(DEPDIR)/parse2.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/parse3.Po ./$(DEPDIR)/parse4.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/reader1.Po ./$(DEPDIR)/reader2.Po \ -@AMDEP_TRUE@ ./$(DEPDIR)/reader3.Po ./$(DEPDIR)/testWriter.Po \ -@AMDEP_TRUE@ ./$(DEPDIR)/tree1.Po ./$(DEPDIR)/tree2.Po \ -@AMDEP_TRUE@ ./$(DEPDIR)/xpath1.Po ./$(DEPDIR)/xpath2.Po +@AMDEP_TRUE@ ./$(DEPDIR)/reader3.Po ./$(DEPDIR)/reader4.Po \ +@AMDEP_TRUE@ ./$(DEPDIR)/testWriter.Po ./$(DEPDIR)/tree1.Po \ +@AMDEP_TRUE@ ./$(DEPDIR)/tree2.Po ./$(DEPDIR)/xpath1.Po \ +@AMDEP_TRUE@ ./$(DEPDIR)/xpath2.Po COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) \ @@ -104,13 +107,13 @@ LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ SOURCES = $(io1_SOURCES) $(io2_SOURCES) $(parse1_SOURCES) \ $(parse2_SOURCES) $(parse3_SOURCES) $(parse4_SOURCES) \ $(reader1_SOURCES) $(reader2_SOURCES) $(reader3_SOURCES) \ - $(testWriter_SOURCES) $(tree1_SOURCES) $(tree2_SOURCES) \ - $(xpath1_SOURCES) $(xpath2_SOURCES) + $(reader4_SOURCES) $(testWriter_SOURCES) $(tree1_SOURCES) \ + $(tree2_SOURCES) $(xpath1_SOURCES) $(xpath2_SOURCES) DIST_SOURCES = $(io1_SOURCES) $(io2_SOURCES) $(parse1_SOURCES) \ $(parse2_SOURCES) $(parse3_SOURCES) $(parse4_SOURCES) \ $(reader1_SOURCES) $(reader2_SOURCES) $(reader3_SOURCES) \ - $(testWriter_SOURCES) $(tree1_SOURCES) $(tree2_SOURCES) \ - $(xpath1_SOURCES) $(xpath2_SOURCES) + $(reader4_SOURCES) $(testWriter_SOURCES) $(tree1_SOURCES) \ + $(tree2_SOURCES) $(xpath1_SOURCES) $(xpath2_SOURCES) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -245,6 +248,7 @@ WITH_PYTHON_FALSE = @WITH_PYTHON_FALSE@ WITH_PYTHON_TRUE = @WITH_PYTHON_TRUE@ WITH_READER = @WITH_READER@ WITH_REGEXPS = @WITH_REGEXPS@ +WITH_RUN_DEBUG = @WITH_RUN_DEBUG@ WITH_SAX1 = @WITH_SAX1@ WITH_SCHEMAS = @WITH_SCHEMAS@ WITH_THREADS = @WITH_THREADS@ @@ -319,7 +323,7 @@ target_alias = @target_alias@ INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include -I@srcdir@/include @THREAD_CFLAGS@ @Z_CFLAGS@ DEPS = $(top_builddir)/libxml2.la LDADDS = @STATIC_BINARIES@ $(top_builddir)/libxml2.la @THREAD_LIBS@ @Z_LIBS@ $(ICONV_LIBS) -lm @WIN32_EXTRA_LIBADD@ -EXTRA_DIST = examples.xsl index.py test1.xml examples.xml test2.xml writer.xml test3.xml tst.xml reader1.res reader3.res tree1.res tree2.res io1.res io2.res xpath1.res xpath2.res +EXTRA_DIST = examples.xsl index.py test1.xml examples.xml test2.xml writer.xml test3.xml tst.xml reader1.res reader3.res tree1.res tree2.res io1.res io2.res xpath1.res xpath2.res reader4.res xpath1_SOURCES = xpath1.c xpath1_LDFLAGS = xpath1_DEPENDENCIES = $(DEPS) @@ -376,6 +380,10 @@ xpath2_SOURCES = xpath2.c xpath2_LDFLAGS = xpath2_DEPENDENCIES = $(DEPS) xpath2_LDADD = @RDL_LIBS@ $(LDADDS) +reader4_SOURCES = reader4.c +reader4_LDFLAGS = +reader4_DEPENDENCIES = $(DEPS) +reader4_LDADD = @RDL_LIBS@ $(LDADDS) all: all-am .SUFFIXES: @@ -443,6 +451,9 @@ reader2$(EXEEXT): $(reader2_OBJECTS) $(reader2_DEPENDENCIES) reader3$(EXEEXT): $(reader3_OBJECTS) $(reader3_DEPENDENCIES) @rm -f reader3$(EXEEXT) $(LINK) $(reader3_LDFLAGS) $(reader3_OBJECTS) $(reader3_LDADD) $(LIBS) +reader4$(EXEEXT): $(reader4_OBJECTS) $(reader4_DEPENDENCIES) + @rm -f reader4$(EXEEXT) + $(LINK) $(reader4_LDFLAGS) $(reader4_OBJECTS) $(reader4_LDADD) $(LIBS) testWriter$(EXEEXT): $(testWriter_OBJECTS) $(testWriter_DEPENDENCIES) @rm -f testWriter$(EXEEXT) $(LINK) $(testWriter_LDFLAGS) $(testWriter_OBJECTS) $(testWriter_LDADD) $(LIBS) @@ -474,6 +485,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reader1.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reader2.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reader3.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reader4.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/testWriter.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tree1.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tree2.Po@am__quote@ @@ -725,6 +737,8 @@ tests: $(noinst_PROGRAMS) @(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0) @($(CHECKER) ./xpath2 test3.xml '//discarded' discarded > xpath2.tmp ; diff xpath2.tmp xpath2.res ; rm xpath2.tmp) @(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0) + @($(CHECKER) ./reader4 test1.xml test2.xml test3.xml > reader4.tmp ; diff reader4.tmp reader4.res ; rm reader4.tmp) + @(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0) # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. .NOEXPORT: diff --git a/doc/examples/examples.xml b/doc/examples/examples.xml index 0d45494..96534fc 100644 --- a/doc/examples/examples.xml +++ b/doc/examples/examples.xml @@ -364,6 +364,26 @@ <function line='94' file='parser' name='xmlParseFile'/> </uses> </example> + <example filename='reader4.c'> + <synopsis>Parse multiple XML files reusing an xmlReader</synopsis> + <purpose>Demonstrate the use of xmlReaderForFile() and xmlReaderNewFile to parse XML files while reusing the reader object and parser context. (Note that the XMLReader functions require libxml2 version later than 2.6.)</purpose> + <usage>reader4 <filename> [ filename ... ]</usage> + <test>reader4 test1.xml test2.xml test3.xml > reader4.tmp ; diff reader4.tmp reader4.res ; rm reader4.tmp</test> + <author>Graham Bennett</author> + <copy>see Copyright for the status of this software. </copy> + <section>xmlReader</section> + <includes> + <include><libxml/xmlreader.h></include> + </includes> + <uses> + <function line='104' file='xmlreader' name='xmlFreeTextReader'/> + <typedef line='54' file='xmlreader' name='xmlTextReaderPtr'/> + <function line='83' file='xmlreader' name='xmlReaderNewFile'/> + <function line='26' file='xmlreader' name='xmlTextReaderRead'/> + <function line='97' file='xmlreader' name='xmlTextReaderCurrentDoc'/> + <function line='72' file='xmlreader' name='xmlReaderForFile'/> + </uses> + </example> <symbols> <symbol name='LIBXML_TEST_VERSION'> <ref filename='xpath1.c'/> @@ -474,6 +494,7 @@ <ref filename='reader1.c'/> <ref filename='reader2.c'/> <ref filename='reader3.c'/> + <ref filename='reader4.c'/> </symbol> <symbol name='xmlFreeTextWriter'> <ref filename='testWriter.c'/> @@ -564,6 +585,10 @@ <ref filename='reader1.c'/> <ref filename='reader2.c'/> <ref filename='reader3.c'/> + <ref filename='reader4.c'/> + </symbol> + <symbol name='xmlReaderNewFile'> + <ref filename='reader4.c'/> </symbol> <symbol name='xmlRealloc'> <ref filename='testWriter.c'/> @@ -597,6 +622,7 @@ </symbol> <symbol name='xmlTextReaderCurrentDoc'> <ref filename='reader3.c'/> + <ref filename='reader4.c'/> </symbol> <symbol name='xmlTextReaderDepth'> <ref filename='reader1.c'/> @@ -624,11 +650,13 @@ <ref filename='reader1.c'/> <ref filename='reader2.c'/> <ref filename='reader3.c'/> + <ref filename='reader4.c'/> </symbol> <symbol name='xmlTextReaderRead'> <ref filename='reader1.c'/> <ref filename='reader2.c'/> <ref filename='reader3.c'/> + <ref filename='reader4.c'/> </symbol> <symbol name='xmlTextWriterEndDocument'> <ref filename='testWriter.c'/> @@ -714,6 +742,7 @@ <example filename='reader1.c'/> <example filename='reader2.c'/> <example filename='reader3.c'/> + <example filename='reader4.c'/> </section> <section name='xmlWriter'> <example filename='testWriter.c'/> diff --git a/doc/examples/index.py b/doc/examples/index.py index 944c181..db41165 100755 --- a/doc/examples/index.py +++ b/doc/examples/index.py @@ -9,7 +9,7 @@ try: import libxml2 except: sys.exit(1) -sys.path.append("..") +sys.path.insert(0, "..") from apibuild import CParser, escape examples = [] diff --git a/doc/examples/reader4.c b/doc/examples/reader4.c new file mode 100644 index 0000000..ed2726a --- /dev/null +++ b/doc/examples/reader4.c @@ -0,0 +1,122 @@ +/** + * section: xmlReader + * synopsis: Parse multiple XML files reusing an xmlReader + * purpose: Demonstrate the use of xmlReaderForFile() and + * xmlReaderNewFile to parse XML files while reusing the reader object + * and parser context. (Note that the XMLReader functions require + * libxml2 version later than 2.6.) + * usage: reader4 <filename> [ filename ... ] + * test: reader4 test1.xml test2.xml test3.xml > reader4.tmp ; diff reader4.tmp reader4.res ; rm reader4.tmp + * author: Graham Bennett + * copy: see Copyright for the status of this software. + */ + +#include <stdio.h> +#include <libxml/xmlreader.h> + +#ifdef LIBXML_READER_ENABLED + +static void processDoc(xmlTextReaderPtr readerPtr) { + int ret; + xmlDocPtr docPtr; + const xmlChar *URL; + + ret = xmlTextReaderRead(readerPtr); + while (ret == 1) { + ret = xmlTextReaderRead(readerPtr); + } + + /* + * One can obtain the document pointer to get insteresting + * information about the document like the URL, but one must also + * be sure to clean it up at the end (see below). + */ + docPtr = xmlTextReaderCurrentDoc(readerPtr); + if (NULL == docPtr) { + fprintf(stderr, "failed to obtain document\n"); + return; + } + + URL = docPtr->URL; + if (NULL == URL) { + fprintf(stderr, "Failed to obtain URL\n"); + } + + if (ret != 0) { + fprintf(stderr, "%s: Failed to parse\n", URL); + return; + } + + printf("%s: Processed ok\n", (const char *)URL); +} + +int main(int argc, char **argv) { + xmlTextReaderPtr readerPtr; + int i; + xmlDocPtr docPtr; + + if (argc < 2) + return(1); + + /* + * this initialises the library and check potential ABI mismatches + * between the version it was compiled for and the actual shared + * library used. + */ + LIBXML_TEST_VERSION + + /* + * Create a new reader for the first file and process the + * document. + */ + readerPtr = xmlReaderForFile(argv[1], NULL, 0); + if (NULL == readerPtr) { + fprintf(stderr, "%s: failed to create reader\n", argv[1]); + return(1); + } + processDoc(readerPtr); + + /* + * The reader can be reused for subsequent files. + */ + for (i=2; i < argc; ++i) { + xmlReaderNewFile(readerPtr, argv[i], NULL, 0); + if (NULL == readerPtr) { + fprintf(stderr, "%s: failed to create reader\n", argv[i]); + return(1); + } + processDoc(readerPtr); + } + + /* + * Since we've called xmlTextReaderCurrentDoc, we now have to + * clean up after ourselves. We only have to do this the last + * time, because xmlReaderNewFile calls xmlCtxtReset which takes + * care of it. + */ + docPtr = xmlTextReaderCurrentDoc(readerPtr); + if (docPtr != NULL) + xmlFreeDoc(docPtr); + + /* + * Clean up the reader. + */ + xmlFreeTextReader(readerPtr); + + /* + * Cleanup function for the XML library. + */ + xmlCleanupParser(); + /* + * this is to debug memory for regression tests + */ + xmlMemoryDump(); + return(0); +} + +#else +int main(void) { + fprintf(stderr, "xmlReader support not compiled in\n"); + exit(1); +} +#endif diff --git a/doc/examples/reader4.res b/doc/examples/reader4.res new file mode 100644 index 0000000..b793f82 --- /dev/null +++ b/doc/examples/reader4.res @@ -0,0 +1,3 @@ +test1.xml: Processed ok +test2.xml: Processed ok +test3.xml: Processed ok |