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 / _ \\ /| '_ \ / _` | __| 9 | __// \| |_) | (_| | |_ 10 \___/_/\_\ .__/ \__,_|\__| 11 |_| XML parser 12 13 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd 14 Copyright (c) 2000-2017 Expat development team 15 Licensed under the MIT license: 16 17 Permission is hereby granted, free of charge, to any person obtaining 18 a copy of this software and associated documentation files (the 19 "Software"), to deal in the Software without restriction, including 20 without limitation the rights to use, copy, modify, merge, publish, 21 distribute, sublicense, and/or sell copies of the Software, and to permit 22 persons to whom the Software is furnished to do so, subject to the 23 following conditions: 24 25 The above copyright notice and this permission notice shall be included 26 in all copies or substantial portions of the Software. 27 28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 31 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 32 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 33 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 34 USE OR OTHER DEALINGS IN THE SOFTWARE. 35 */ 36 37 #include <stdio.h> 38 #include <expat.h> 39 40 #ifdef XML_LARGE_SIZE 41 # define XML_FMT_INT_MOD "ll" 42 #else 43 # define XML_FMT_INT_MOD "l" 44 #endif 45 46 #ifdef XML_UNICODE_WCHAR_T 47 # include <wchar.h> 48 # define XML_FMT_STR "ls" 49 #else 50 # define XML_FMT_STR "s" 51 #endif 52 53 static void XMLCALL 54 startElement(void *userData, const XML_Char *name, const XML_Char **atts) { 55 int i; 56 int *depthPtr = (int *)userData; 57 (void)atts; 58 59 for (i = 0; i < *depthPtr; i++) 60 putchar('\t'); 61 printf("%" XML_FMT_STR "\n", name); 62 *depthPtr += 1; 63 } 64 65 static void XMLCALL 66 endElement(void *userData, const XML_Char *name) { 67 int *depthPtr = (int *)userData; 68 (void)name; 69 70 *depthPtr -= 1; 71 } 72 73 int 74 main(int argc, char *argv[]) { 75 char buf[BUFSIZ]; 76 XML_Parser parser = XML_ParserCreate(NULL); 77 int done; 78 int depth = 0; 79 (void)argc; 80 (void)argv; 81 82 XML_SetUserData(parser, &depth); 83 XML_SetElementHandler(parser, startElement, endElement); 84 do { 85 size_t len = fread(buf, 1, sizeof(buf), stdin); 86 done = len < sizeof(buf); 87 if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) { 88 fprintf(stderr, "%" XML_FMT_STR " at line %" XML_FMT_INT_MOD "u\n", 89 XML_ErrorString(XML_GetErrorCode(parser)), 90 XML_GetCurrentLineNumber(parser)); 91 XML_ParserFree(parser); 92 return 1; 93 } 94 } while (! done); 95 XML_ParserFree(parser); 96 return 0; 97 } 98