2001-07-26 17:54:43 -04:00
|
|
|
/* This is simple demonstration of how to use expat. This program
|
2002-07-01 10:45:51 -04:00
|
|
|
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.
|
|
|
|
*/
|
2001-07-26 17:54:43 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "expat.h"
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2001-07-26 17:54:43 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2001-07-26 17:54:43 -04:00
|
|
|
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);
|
2002-08-02 15:40:09 -04:00
|
|
|
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
|
2001-07-26 17:54:43 -04:00
|
|
|
fprintf(stderr,
|
2002-07-01 10:45:51 -04:00
|
|
|
"%s at line %d\n",
|
|
|
|
XML_ErrorString(XML_GetErrorCode(parser)),
|
|
|
|
XML_GetCurrentLineNumber(parser));
|
2001-07-26 17:54:43 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} while (!done);
|
|
|
|
XML_ParserFree(parser);
|
|
|
|
return 0;
|
|
|
|
}
|