diff --git a/expat/examples/.gitignore b/expat/examples/.gitignore index b8b6bd1c..2ee5ec2b 100644 --- a/expat/examples/.gitignore +++ b/expat/examples/.gitignore @@ -1,2 +1,3 @@ Makefile +elements outline diff --git a/expat/examples/Makefile.in b/expat/examples/Makefile.in index 6c4e9def..93fd64ec 100644 --- a/expat/examples/Makefile.in +++ b/expat/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/expat/examples/elements.c b/expat/examples/elements.c new file mode 100644 index 00000000..9a20c34d --- /dev/null +++ b/expat/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; +}