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.
8 / _ \\ /| '_ \ / _` | __|
9 | __// \| |_) | (_| | |_
10 \___/_/\_\ .__/ \__,_|\__|
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:
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
25 The above copyright notice and this permission notice shall be included
26 in all copies or substantial portions of the Software.
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.
41 # if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
42 # define XML_FMT_INT_MOD "I64"
44 # define XML_FMT_INT_MOD "ll"
47 # define XML_FMT_INT_MOD "l"
50 #ifdef XML_UNICODE_WCHAR_T
52 # define XML_FMT_STR "ls"
54 # define XML_FMT_STR "s"
58 startElement(void *userData, const XML_Char *name, const XML_Char **atts)
61 int *depthPtr = (int *)userData;
64 for (i = 0; i < *depthPtr; i++)
66 printf("%" XML_FMT_STR "\n", name);
71 endElement(void *userData, const XML_Char *name)
73 int *depthPtr = (int *)userData;
80 main(int argc, char *argv[])
83 XML_Parser parser = XML_ParserCreate(NULL);
89 XML_SetUserData(parser, &depth);
90 XML_SetElementHandler(parser, startElement, endElement);
92 size_t len = fread(buf, 1, sizeof(buf), stdin);
93 done = len < sizeof(buf);
94 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
96 "%" XML_FMT_STR " at line %" XML_FMT_INT_MOD "u\n",
97 XML_ErrorString(XML_GetErrorCode(parser)),
98 XML_GetCurrentLineNumber(parser));
102 XML_ParserFree(parser);