Move the "elements" example code here from "sample", so we only have one

directory of short sample code.
This commit is contained in:
Fred L. Drake, Jr. 2001-07-26 21:54:43 +00:00
parent d8263250ee
commit 86b7ae104d
3 changed files with 56 additions and 3 deletions

View file

@ -1,2 +1,3 @@
Makefile
elements
outline

View 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

49
expat/examples/elements.c Normal file
View file

@ -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;
}