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 25static void XMLCALL 26startElement(void *userData, const char *name, const char **atts) 27{ 28 int i; 29 int *depthPtr = (int *)userData; 30 (void)atts; 31 32 for (i = 0; i < *depthPtr; i++) 33 putchar('\t'); 34 puts(name); 35 *depthPtr += 1; 36} 37 38static void XMLCALL 39endElement(void *userData, const char *name) 40{ 41 int *depthPtr = (int *)userData; 42 (void)name; 43 44 *depthPtr -= 1; 45} 46 47int 48main(int argc, char *argv[]) 49{ 50 char buf[BUFSIZ]; 51 XML_Parser parser = XML_ParserCreate(NULL); 52 int done; 53 int depth = 0; 54 (void)argc; 55 (void)argv; 56 57 XML_SetUserData(parser, &depth); 58 XML_SetElementHandler(parser, startElement, endElement); 59 do { 60 size_t len = fread(buf, 1, sizeof(buf), stdin); 61 done = len < sizeof(buf); 62 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) { 63 fprintf(stderr, 64 "%s at line %" XML_FMT_INT_MOD "u\n", 65 XML_ErrorString(XML_GetErrorCode(parser)), 66 XML_GetCurrentLineNumber(parser)); 67 return 1; 68 } 69 } while (!done); 70 XML_ParserFree(parser); 71 return 0; 72} 73