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.
|
2005-12-28 13:43:36 -05:00
|
|
|
It must be used with Expat compiled for UTF-8 output.
|
2002-07-01 10:45:51 -04:00
|
|
|
*/
|
2001-07-26 17:54:43 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-06-07 14:27:02 -04:00
|
|
|
#include <expat.h>
|
2001-07-26 17:54:43 -04:00
|
|
|
|
2005-12-28 13:43:36 -05:00
|
|
|
#ifdef XML_LARGE_SIZE
|
|
|
|
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
|
|
|
|
#define XML_FMT_INT_MOD "I64"
|
|
|
|
#else
|
|
|
|
#define XML_FMT_INT_MOD "ll"
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define XML_FMT_INT_MOD "l"
|
|
|
|
#endif
|
|
|
|
|
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;
|
2004-08-10 14:06:29 -04:00
|
|
|
int *depthPtr = (int *)userData;
|
2016-05-07 10:45:58 -04:00
|
|
|
(void)atts;
|
|
|
|
|
2001-07-26 17:54:43 -04:00
|
|
|
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)
|
|
|
|
{
|
2004-08-10 14:06:29 -04:00
|
|
|
int *depthPtr = (int *)userData;
|
2016-05-07 10:45:58 -04:00
|
|
|
(void)name;
|
|
|
|
|
2001-07-26 17:54:43 -04:00
|
|
|
*depthPtr -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
XML_Parser parser = XML_ParserCreate(NULL);
|
|
|
|
int done;
|
|
|
|
int depth = 0;
|
2016-05-07 10:45:58 -04:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
2001-07-26 17:54:43 -04:00
|
|
|
XML_SetUserData(parser, &depth);
|
|
|
|
XML_SetElementHandler(parser, startElement, endElement);
|
|
|
|
do {
|
2016-03-11 20:59:53 -05:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), stdin);
|
2001-07-26 17:54:43 -04:00
|
|
|
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,
|
2005-12-28 13:43:36 -05:00
|
|
|
"%s at line %" XML_FMT_INT_MOD "u\n",
|
2002-07-01 10:45:51 -04:00
|
|
|
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;
|
|
|
|
}
|