]> granicus.if.org Git - libexpat/commitdiff
Move the "elements" example code here from "sample", so we only have one
authorFred L. Drake, Jr. <fdrake@users.sourceforge.net>
Thu, 26 Jul 2001 21:54:43 +0000 (21:54 +0000)
committerFred L. Drake, Jr. <fdrake@users.sourceforge.net>
Thu, 26 Jul 2001 21:54:43 +0000 (21:54 +0000)
directory of short sample code.

expat/examples/.gitignore
expat/examples/Makefile.in
expat/examples/elements.c [new file with mode: 0644]

index b8b6bd1ceb6c99836bb2cac9c346923ee5811437..2ee5ec2b99037d79113b01b769297aa4e5a3beda 100644 (file)
@@ -1,2 +1,3 @@
 Makefile
+elements
 outline
index 6c4e9def27692965dc8b66b9fab238dd2a68ab0d..93fd64ecd1a2c8395764bff9c97835b6c4760727 100644 (file)
@@ -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 (file)
index 0000000..9a20c34
--- /dev/null
@@ -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;
+}