1104349Sphk/* This is simple demonstration of how to use expat. This program 2104349Sphk reads an XML document from standard input and writes a line with 3104349Sphk the name of each element to standard output indenting child 4104349Sphk elements by one tab stop more than their parent element. 5178848Scokane It must be used with Expat compiled for UTF-8 output. 6104349Sphk*/ 7104349Sphk 8104349Sphk#include <stdio.h> 9104349Sphk#include "expat.h" 10104349Sphk 11178848Scokane#if defined(__amigaos__) && defined(__USE_INLINE__) 12178848Scokane#include <proto/expat.h> 13178848Scokane#endif 14178848Scokane 15178848Scokane#ifdef XML_LARGE_SIZE 16178848Scokane#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400 17178848Scokane#define XML_FMT_INT_MOD "I64" 18178848Scokane#else 19178848Scokane#define XML_FMT_INT_MOD "ll" 20178848Scokane#endif 21178848Scokane#else 22178848Scokane#define XML_FMT_INT_MOD "l" 23178848Scokane#endif 24178848Scokane 25178848Scokanestatic void XMLCALL 26104349SphkstartElement(void *userData, const char *name, const char **atts) 27104349Sphk{ 28104349Sphk int i; 29178848Scokane int *depthPtr = (int *)userData; 30104349Sphk for (i = 0; i < *depthPtr; i++) 31104349Sphk putchar('\t'); 32104349Sphk puts(name); 33104349Sphk *depthPtr += 1; 34104349Sphk} 35104349Sphk 36178848Scokanestatic void XMLCALL 37104349SphkendElement(void *userData, const char *name) 38104349Sphk{ 39178848Scokane int *depthPtr = (int *)userData; 40104349Sphk *depthPtr -= 1; 41104349Sphk} 42104349Sphk 43104349Sphkint 44104349Sphkmain(int argc, char *argv[]) 45104349Sphk{ 46104349Sphk char buf[BUFSIZ]; 47104349Sphk XML_Parser parser = XML_ParserCreate(NULL); 48104349Sphk int done; 49104349Sphk int depth = 0; 50104349Sphk XML_SetUserData(parser, &depth); 51104349Sphk XML_SetElementHandler(parser, startElement, endElement); 52104349Sphk do { 53178848Scokane int len = (int)fread(buf, 1, sizeof(buf), stdin); 54104349Sphk done = len < sizeof(buf); 55104349Sphk if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) { 56104349Sphk fprintf(stderr, 57178848Scokane "%s at line %" XML_FMT_INT_MOD "u\n", 58104349Sphk XML_ErrorString(XML_GetErrorCode(parser)), 59104349Sphk XML_GetCurrentLineNumber(parser)); 60104349Sphk return 1; 61104349Sphk } 62104349Sphk } while (!done); 63104349Sphk XML_ParserFree(parser); 64104349Sphk return 0; 65104349Sphk} 66