diff options
author | 2004-09-22 21:32:02 +0000 | |
---|---|---|
committer | 2004-09-22 21:32:02 +0000 | |
commit | 1a3ddf8c85cebf76fa2d1513d8563d9aa7701399 (patch) | |
tree | 2d69702ff97396421f9b46fc82403ce2e6a9f340 /lib/libexpat/examples/elements.c | |
parent | driver for the "central" controller on the E4500 (and others) [fhc and others to follow] (diff) | |
download | wireguard-openbsd-1a3ddf8c85cebf76fa2d1513d8563d9aa7701399.tar.xz wireguard-openbsd-1a3ddf8c85cebf76fa2d1513d8563d9aa7701399.zip |
libexpat: a simple xml parser library, used by X11 and some ports.
Diffstat (limited to 'lib/libexpat/examples/elements.c')
-rw-r--r-- | lib/libexpat/examples/elements.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/libexpat/examples/elements.c b/lib/libexpat/examples/elements.c new file mode 100644 index 00000000000..4ed4da6a346 --- /dev/null +++ b/lib/libexpat/examples/elements.c @@ -0,0 +1,50 @@ +/* This is simple demonstration of how to use expat. This program + reads an XML document from standard input and writes a line with + the name of each element to standard output indenting child + elements by one tab stop more than their parent element. +*/ + +#include <stdio.h> +#include "expat.h" + +static void +startElement(void *userData, const char *name, const char **atts) +{ + int i; + int *depthPtr = userData; + for (i = 0; i < *depthPtr; i++) + putchar('\t'); + puts(name); + *depthPtr += 1; +} + +static void +endElement(void *userData, const char *name) +{ + int *depthPtr = userData; + *depthPtr -= 1; +} + +int +main(int argc, char *argv[]) +{ + char buf[BUFSIZ]; + XML_Parser parser = XML_ParserCreate(NULL); + int done; + int depth = 0; + XML_SetUserData(parser, &depth); + XML_SetElementHandler(parser, startElement, endElement); + do { + size_t len = fread(buf, 1, sizeof(buf), stdin); + done = len < sizeof(buf); + if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) { + fprintf(stderr, + "%s at line %d\n", + XML_ErrorString(XML_GetErrorCode(parser)), + XML_GetCurrentLineNumber(parser)); + return 1; + } + } while (!done); + XML_ParserFree(parser); + return 0; +} |