172
|
1 |
/* This is simple demonstration of how to use expat. This program
|
|
2 |
reads an XML document from standard input and writes a line with
|
|
3 |
the name of each element to standard output indenting child
|
|
4 |
elements by one tab stop more than their parent element.
|
|
5 |
It must be used with Expat compiled for UTF-8 output.
|
|
6 |
*/
|
|
7 |
|
|
8 |
#include <stdio.h>
|
|
9 |
#include "expat.h"
|
|
10 |
|
|
11 |
#if defined(__amigaos__) && defined(__USE_INLINE__)
|
|
12 |
#include <proto/expat.h>
|
|
13 |
#endif
|
|
14 |
|
|
15 |
#ifdef XML_LARGE_SIZE
|
|
16 |
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
|
|
17 |
#define XML_FMT_INT_MOD "I64"
|
|
18 |
#else
|
|
19 |
#define XML_FMT_INT_MOD "ll"
|
|
20 |
#endif
|
|
21 |
#else
|
|
22 |
#define XML_FMT_INT_MOD "l"
|
|
23 |
#endif
|
|
24 |
|
|
25 |
static void XMLCALL
|
|
26 |
startElement(void *userData, const char *name, const char **atts)
|
|
27 |
{
|
|
28 |
int i;
|
|
29 |
int *depthPtr = (int *)userData;
|
|
30 |
for (i = 0; i < *depthPtr; i++)
|
|
31 |
putchar('\t');
|
|
32 |
puts(name);
|
|
33 |
*depthPtr += 1;
|
|
34 |
}
|
|
35 |
|
|
36 |
static void XMLCALL
|
|
37 |
endElement(void *userData, const char *name)
|
|
38 |
{
|
|
39 |
int *depthPtr = (int *)userData;
|
|
40 |
*depthPtr -= 1;
|
|
41 |
}
|
|
42 |
|
|
43 |
int
|
|
44 |
main(int argc, char *argv[])
|
|
45 |
{
|
|
46 |
char buf[BUFSIZ];
|
|
47 |
XML_Parser parser = XML_ParserCreate(NULL);
|
|
48 |
int done;
|
|
49 |
int depth = 0;
|
|
50 |
XML_SetUserData(parser, &depth);
|
|
51 |
XML_SetElementHandler(parser, startElement, endElement);
|
|
52 |
do {
|
|
53 |
int len = (int)fread(buf, 1, sizeof(buf), stdin);
|
|
54 |
done = len < sizeof(buf);
|
|
55 |
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
|
|
56 |
fprintf(stderr,
|
|
57 |
"%s at line %" XML_FMT_INT_MOD "u\n",
|
|
58 |
XML_ErrorString(XML_GetErrorCode(parser)),
|
|
59 |
XML_GetCurrentLineNumber(parser));
|
|
60 |
return 1;
|
|
61 |
}
|
|
62 |
} while (!done);
|
|
63 |
XML_ParserFree(parser);
|
|
64 |
return 0;
|
|
65 |
}
|