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-2022 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 # define XML_FMT_STR "ls" 53 #else 54 # define XML_FMT_STR "s" 55 #endif 56 57 static void XMLCALL 58 startElement(void *userData, const XML_Char *name, const XML_Char **atts) { 59 int i; 60 int *const depthPtr = (int *)userData; 61 (void)atts; 62 63 for (i = 0; i < *depthPtr; i++) 64 putchar('\t'); 65 printf("%" XML_FMT_STR "\n", name); 66 *depthPtr += 1; 67 } 68 69 static void XMLCALL 70 endElement(void *userData, const XML_Char *name) { 71 int *const depthPtr = (int *)userData; 72 (void)name; 73 74 *depthPtr -= 1; 75 } 76 77 int 78 main(void) { 79 XML_Parser parser = XML_ParserCreate(NULL); 80 int done; 81 int depth = 0; 82 83 if (! parser) { 84 fprintf(stderr, "Couldn't allocate memory for parser\n"); 85 return 1; 86 } 87 88 XML_SetUserData(parser, &depth); 89 XML_SetElementHandler(parser, startElement, endElement); 90 91 do { 92 void *const buf = XML_GetBuffer(parser, BUFSIZ); 93 if (! buf) { 94 fprintf(stderr, "Couldn't allocate memory for buffer\n"); 95 XML_ParserFree(parser); 96 return 1; 97 } 98 99 const size_t len = fread(buf, 1, BUFSIZ, stdin); 100 101 if (ferror(stdin)) { 102 fprintf(stderr, "Read error\n"); 103 XML_ParserFree(parser); 104 return 1; 105 } 106 107 done = feof(stdin); 108 109 if (XML_ParseBuffer(parser, (int)len, done) == XML_STATUS_ERROR) { 110 fprintf(stderr, 111 "Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n", 112 XML_GetCurrentLineNumber(parser), 113 XML_ErrorString(XML_GetErrorCode(parser))); 114 XML_ParserFree(parser); 115 return 1; 116 } 117 } while (! done); 118 119 XML_ParserFree(parser); 120 return 0; 121 } 122