summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorfdrake <fdrake>2001-07-26 21:54:43 +0000
committerfdrake <fdrake>2001-07-26 21:54:43 +0000
commit51add8cbb77669869855e168f5581a0ad44d9522 (patch)
treecb283ecd4d9e5fa37aa35c149d0c14a577747896 /examples
parentdf19def73628c1fe3dc0027ca0f992645d6f6131 (diff)
downloadlibexpat-51add8cbb77669869855e168f5581a0ad44d9522.tar.gz
Move the "elements" example code here from "sample", so we only have one
directory of short sample code.
Diffstat (limited to 'examples')
-rw-r--r--examples/.cvsignore1
-rw-r--r--examples/Makefile.in9
-rw-r--r--examples/elements.c49
3 files changed, 56 insertions, 3 deletions
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 <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)) {
+ fprintf(stderr,
+ "%s at line %d\n",
+ XML_ErrorString(XML_GetErrorCode(parser)),
+ XML_GetCurrentLineNumber(parser));
+ return 1;
+ }
+ } while (!done);
+ XML_ParserFree(parser);
+ return 0;
+}