The simple XML parser comes with two simple demo applications showing
its use. To build and test them simply perform the following steps:
- If necessary modify the Makefiles in the toplevel and the
demo directory according to your system (this shouldn't be necessary)
- % make
- % cd demo
- % make run
The demo applications included are: - dump
-
A simple dumper that reads an XML file and dumps what it has read
to standard out. This example shows the events generated by the parser.
- group
-
An application that reads an XML group file that contains persons and
groups of persons. You may perform simple queries (such as list all
persons in a group). In addition it shows how one may re-use the
simple XML parser once it's been created.
(Please note that in real
life one would specify this XML file differently and e.g. use a DTD
to prevent the two-pass parsing but this is an example to illustrate
the use of the parser and quite ok for this purpose ;-).)
The parser only consists of the two files simplexml.c and
simplexml.h . That's it. To use it, simply add the two files to your application and compile
it with them (it doesn't make sense to have a library for simplexml since
the whole purpose of simplexml is to leave it as simple that you can
statically add it to your application). The parser itself only uses the standard and the string library and
should thus compile on almost any platform. (Well, to be honest, I
haven't written any C code in years. I'm sure you're going to notice
a lot of stuff where you'd say "awww, this must have been made by an
OO freak, one wouldn't do it like this in C". Well, feel free to
correct the code, and if you do please send me a copy of it ;-)) Using the Simple XML parser is really simple:
- Include the parser in your source:
#include "simplexml.h"
- Read all the data you want to parse to a character array:
char* sData= ...; /* the data to parse */
long nDataLen= ...; /* the number of characters to parse */
- Create a parser:
SimpleXmlParser parser= simpleXmlCreateParser(sData, nDataLen);
- Create a handler for parser events:
void* handler (SimpleXmlParser parser, SimpleXmlEvent event,
const char* szName, const char* szValue) {
... /* do something */
return handler;
}
- Invoke the parser:
simpleXmlParse(parser, handler);
Note: The beauty of Simple XML lies in the fact the handler may return
a different handler for every subtag read, i.e. you may write a
handler for every tag type. Take a look at the 'group.c' sample, it
makes use of this feature. To get an idea of how to use something I always prefer demo code (it's
the best way to learn something for me). That's why I've included two
demo programs: 'dump' and 'group'. 'dump' simply shows off what the
parser does by dumping out the various parser events. It does not make
use of the handler-switching-feature and is therefore not a very good
example. 'group' on the other hand makes use of Simple XML the way it
was intended. It reads persons and person groups from an XML file. For a concise API documentation please refer to the comments in the
header file 'simplexml.h' (the comments are written in sort of a
JavaDoc style). |