Commit 88155d85 authored by Daniel Veillard's avatar Daniel Veillard
Browse files

Added a couple of parsing examples, Daniel

parent ebe25d49
......@@ -11,3 +11,5 @@ reader1
reader2
reader3
io1
parse3
parse4
......@@ -17,7 +17,7 @@ install-data-local:
EXTRA_DIST=examples.xsl index.py test1.xml examples.xml test2.xml writer.xml test3.xml reader1.res reader3.res tree1.res tree2.res io1.res
noinst_PROGRAMS=xpath1 parse1 parse2 tree1 tree2 testWriter reader1 reader2 reader3 io1
noinst_PROGRAMS=xpath1 parse1 parse2 tree1 tree2 testWriter reader1 reader2 reader3 io1 parse3 parse4
xpath1_SOURCES=xpath1.c
xpath1_LDFLAGS=
......@@ -69,6 +69,16 @@ io1_LDFLAGS=
io1_DEPENDENCIES= $(DEPS)
io1_LDADD= @RDL_LIBS@ $(LDADDS)
parse3_SOURCES=parse3.c
parse3_LDFLAGS=
parse3_DEPENDENCIES= $(DEPS)
parse3_LDADD= @RDL_LIBS@ $(LDADDS)
parse4_SOURCES=parse4.c
parse4_LDFLAGS=
parse4_DEPENDENCIES= $(DEPS)
parse4_LDADD= @RDL_LIBS@ $(LDADDS)
tests: $(noinst_PROGRAMS)
@(echo > .memdump)
@(./parse1 test1.xml)
......@@ -89,5 +99,9 @@ tests: $(noinst_PROGRAMS)
@(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0)
@(./io1 > io1.tmp ; diff io1.tmp io1.res ; rm -f io1.tmp)
@(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0)
@(./parse3)
@(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0)
@(./parse4 test3.xml)
@(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0)
......@@ -279,6 +279,41 @@
<typedef line='105' file='tree' name='xmlDocPtr'/>
</uses>
</example>
<example filename='parse3.c'>
<synopsis>Parse an XML document in memory to a tree and free it</synopsis>
<purpose>Demonstrate the use of xmlReadMemory() to read an XML file into a tree and and xmlFreeDoc() to free the resulting tree</purpose>
<usage>parse3</usage>
<test>parse3</test>
<author>Daniel Veillard</author>
<copy>see Copyright for the status of this software. </copy>
<section>Parsing</section>
<includes>
<include>&lt;libxml/tree.h&gt;</include>
<include>&lt;libxml/parser.h&gt;</include>
</includes>
<uses>
<function line='33' file='parser' name='xmlReadMemory'/>
</uses>
</example>
<example filename='parse4.c'>
<synopsis>Parse an XML document chunk by chunk to a tree and free it</synopsis>
<purpose>Demonstrate the use of xmlCreatePushParserCtxt() and xmlParseChunk() to read an XML file progressively into a tree and and xmlFreeDoc() to free the resulting tree</purpose>
<usage>parse4 test3.xml</usage>
<test>parse4 test3.xml</test>
<author>Daniel Veillard</author>
<copy>see Copyright for the status of this software. </copy>
<section>Parsing</section>
<includes>
<include>&lt;libxml/tree.h&gt;</include>
<include>&lt;libxml/parser.h&gt;</include>
</includes>
<uses>
<function line='94' file='parser' name='xmlFreeParserCtxt'/>
<function line='86' file='parser' name='xmlParseChunk'/>
<function line='67' file='parser' name='xmlCreatePushParserCtxt'/>
<typedef line='44' file='tree' name='xmlParserCtxtPtr'/>
</uses>
</example>
<symbols>
<symbol name='LIBXML_TEST_VERSION'>
<ref filename='xpath1.c'/>
......@@ -334,6 +369,9 @@
<symbol name='xmlCreateIntSubset'>
<ref filename='tree2.c'/>
</symbol>
<symbol name='xmlCreatePushParserCtxt'>
<ref filename='parse4.c'/>
</symbol>
<symbol name='xmlCtxtReadFile'>
<ref filename='parse2.c'/>
</symbol>
......@@ -373,6 +411,7 @@
</symbol>
<symbol name='xmlFreeParserCtxt'>
<ref filename='parse2.c'/>
<ref filename='parse4.c'/>
</symbol>
<symbol name='xmlFreeTextReader'>
<ref filename='reader1.c'/>
......@@ -437,18 +476,23 @@
<symbol name='xmlNsPtr'>
<ref filename='xpath1.c'/>
</symbol>
<symbol name='xmlParseChunk'>
<ref filename='parse4.c'/>
</symbol>
<symbol name='xmlParseFile'>
<ref filename='xpath1.c'/>
<ref filename='tree1.c'/>
</symbol>
<symbol name='xmlParserCtxtPtr'>
<ref filename='parse2.c'/>
<ref filename='parse4.c'/>
</symbol>
<symbol name='xmlReadFile'>
<ref filename='parse1.c'/>
</symbol>
<symbol name='xmlReadMemory'>
<ref filename='io1.c'/>
<ref filename='parse3.c'/>
</symbol>
<symbol name='xmlReaderForFile'>
<ref filename='reader1.c'/>
......@@ -582,6 +626,8 @@
<section name='Parsing'>
<example filename='parse1.c'/>
<example filename='parse2.c'/>
<example filename='parse3.c'/>
<example filename='parse4.c'/>
</section>
<section name='Tree'>
<example filename='tree1.c'/>
......
This diff is collapsed.
/**
* section: Parsing
* synopsis: Parse an XML document in memory to a tree and free it
* purpose: Demonstrate the use of xmlReadMemory() to read an XML file
* into a tree and and xmlFreeDoc() to free the resulting tree
* usage: parse3
* test: parse3
* author: Daniel Veillard
* copy: see Copyright for the status of this software.
*/
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
static const char *document = "<doc/>";
/**
* example3Func:
* @content: the content of the document
* @length: the length in bytes
*
* Parse the in memory document and free the resulting tree
*/
static void
example3Func(const char *content, int length) {
xmlDocPtr doc; /* the resulting document tree */
/*
* The document being in memory, it have no base per RFC 2396,
* and the "noname.xml" argument will serve as its base.
*/
doc = xmlReadMemory(content, length, "noname.xml", NULL, 0);
if (doc == NULL) {
fprintf(stderr, "Failed to parse document\n");
return;
}
xmlFreeDoc(doc);
}
int main(void) {
/*
* 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
example3Func(document, 6);
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
return(0);
}
/**
* section: Parsing
* synopsis: Parse an XML document chunk by chunk to a tree and free it
* purpose: Demonstrate the use of xmlCreatePushParserCtxt() and
* xmlParseChunk() to read an XML file progressively
* into a tree and and xmlFreeDoc() to free the resulting tree
* usage: parse4 test3.xml
* test: parse4 test3.xml
* author: Daniel Veillard
* copy: see Copyright for the status of this software.
*/
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
static FILE *desc;
/**
* readPacket:
* @mem: array to store the packet
* @size: the packet size
*
* read at most @size bytes from the document and store it in @mem
*
* Returns the number of bytes read
*/
static int
readPacket(char *mem, int size) {
int res;
res = fread(mem, 1, size, desc);
return(res);
}
/**
* example4Func:
* @filename: a filename or an URL
*
* Parse the resource and free the resulting tree
*/
static void
example4Func(const char *filename) {
xmlParserCtxtPtr ctxt;
char chars[4];
int size = 4;
xmlDocPtr doc; /* the resulting document tree */
int res;
/*
* Read a few first byte to check the input used for the
* encoding detection at the parser level.
*/
res = readPacket(chars, 4);
if (res <= 0) {
fprintf(stderr, "Failed to parse %s\n", filename);
return;
}
/*
* Create a progressive parsing context, the 2 first arguments
* are not used since we want to build a tree and not use a SAX
* parsing interface. We also pass the first bytes of the document
* to allow encoding detection when creating the parser but this
* is optional.
*/
ctxt = xmlCreatePushParserCtxt(NULL, NULL,
chars, res, filename);
if (ctxt == NULL) {
fprintf(stderr, "Failed to create parser context !\n");
return;
}
/*
* loop on the input getting the document data, of course 4 bytes
* at a time is not realistic but allows to verify testing on small
* documents.
*/
while ((res = readPacket(chars, 4)) > 0) {
xmlParseChunk(ctxt, chars, res, 0);
}
/*
* there is no more input, indicate the parsing is finished.
*/
xmlParseChunk(ctxt, chars, 0, 1);
/*
* collect the document back and if it was wellformed
* and destroy the parser context.
*/
doc = ctxt->myDoc;
res = ctxt->wellFormed;
xmlFreeParserCtxt(ctxt);
if (!res) {
fprintf(stderr, "Failed to parse %s\n", filename);
}
/*
* since we don't use the document, destroy it now.
*/
xmlFreeDoc(doc);
}
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
/*
* simulate a progressive parsing using the input file.
*/
desc = fopen(argv[1], "rb");
if (desc != NULL) {
example4Func(argv[1]);
fclose(desc);
} else {
fprintf(stderr, "Failed to parse %s\n", argv[1]);
}
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
return(0);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment