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