From 51add8cbb77669869855e168f5581a0ad44d9522 Mon Sep 17 00:00:00 2001 From: fdrake Date: Thu, 26 Jul 2001 21:54:43 +0000 Subject: Move the "elements" example code here from "sample", so we only have one directory of short sample code. --- examples/.cvsignore | 1 + examples/Makefile.in | 9 ++++++--- examples/elements.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 examples/elements.c (limited to 'examples') diff --git a/examples/.cvsignore b/examples/.cvsignore index b8b6bd1..2ee5ec2 100644 --- a/examples/.cvsignore +++ b/examples/.cvsignore @@ -1,2 +1,3 @@ Makefile +elements outline diff --git a/examples/Makefile.in b/examples/Makefile.in index 6c4e9de..93fd64e 100644 --- a/examples/Makefile.in +++ b/examples/Makefile.in @@ -32,10 +32,13 @@ top_srcdir = @top_srcdir@ VPATH = @srcdir@ -all: outline +all: elements outline + +elements: elements.o + $(CC) -o $@ $< $(LDFLAGS) $(LIBS) outline: outline.o - $(CC) -o outline outline.o $(LDFLAGS) $(LIBS) + $(CC) -o $@ $< $(LDFLAGS) $(LIBS) check: $(SUBDIRS) @echo @@ -43,7 +46,7 @@ check: $(SUBDIRS) @echo clean: - rm -f outline core *.o + rm -f elements outline core *.o distclean: clean rm -f Makefile diff --git a/examples/elements.c b/examples/elements.c new file mode 100644 index 0000000..9a20c34 --- /dev/null +++ b/examples/elements.c @@ -0,0 +1,49 @@ +/* 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 +#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)) { + fprintf(stderr, + "%s at line %d\n", + XML_ErrorString(XML_GetErrorCode(parser)), + XML_GetCurrentLineNumber(parser)); + return 1; + } + } while (!done); + XML_ParserFree(parser); + return 0; +} -- cgit v1.2.1