2001-07-26 17:54:43 -04:00
|
|
|
/* This is simple demonstration of how to use expat. This program
|
2002-07-01 10:45:51 -04:00
|
|
|
reads an XML document from standard input and writes a line with
|
|
|
|
the name of each element to standard output indenting child
|
|
|
|
elements by one tab stop more than their parent element.
|
2005-12-28 13:43:36 -05:00
|
|
|
It must be used with Expat compiled for UTF-8 output.
|
2017-08-12 14:56:14 -04:00
|
|
|
__ __ _
|
|
|
|
___\ \/ /_ __ __ _| |_
|
|
|
|
/ _ \\ /| '_ \ / _` | __|
|
|
|
|
| __// \| |_) | (_| | |_
|
|
|
|
\___/_/\_\ .__/ \__,_|\__|
|
|
|
|
|_| XML parser
|
|
|
|
|
|
|
|
Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
|
|
|
|
Copyright (c) 2000-2017 Expat development team
|
|
|
|
Licensed under the MIT license:
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
persons to whom the Software is furnished to do so, subject to the
|
|
|
|
following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2002-07-01 10:45:51 -04:00
|
|
|
*/
|
2001-07-26 17:54:43 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-06-07 14:27:02 -04:00
|
|
|
#include <expat.h>
|
2001-07-26 17:54:43 -04:00
|
|
|
|
2005-12-28 13:43:36 -05:00
|
|
|
#ifdef XML_LARGE_SIZE
|
2019-08-29 12:51:03 -04:00
|
|
|
# define XML_FMT_INT_MOD "ll"
|
2005-12-28 13:43:36 -05:00
|
|
|
#else
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_INT_MOD "l"
|
2005-12-28 13:43:36 -05:00
|
|
|
#endif
|
|
|
|
|
2017-08-24 09:16:16 -04:00
|
|
|
#ifdef XML_UNICODE_WCHAR_T
|
2019-08-03 14:34:54 -04:00
|
|
|
# include <wchar.h>
|
|
|
|
# define XML_FMT_STR "ls"
|
2017-08-24 09:16:16 -04:00
|
|
|
#else
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_STR "s"
|
2017-08-24 09:16:16 -04:00
|
|
|
#endif
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
|
2001-07-26 17:54:43 -04:00
|
|
|
int i;
|
2004-08-10 14:06:29 -04:00
|
|
|
int *depthPtr = (int *)userData;
|
2016-05-07 10:45:58 -04:00
|
|
|
(void)atts;
|
|
|
|
|
2001-07-26 17:54:43 -04:00
|
|
|
for (i = 0; i < *depthPtr; i++)
|
|
|
|
putchar('\t');
|
2017-08-29 09:05:38 -04:00
|
|
|
printf("%" XML_FMT_STR "\n", name);
|
2001-07-26 17:54:43 -04:00
|
|
|
*depthPtr += 1;
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
endElement(void *userData, const XML_Char *name) {
|
2004-08-10 14:06:29 -04:00
|
|
|
int *depthPtr = (int *)userData;
|
2016-05-07 10:45:58 -04:00
|
|
|
(void)name;
|
|
|
|
|
2001-07-26 17:54:43 -04:00
|
|
|
*depthPtr -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-08-03 14:34:54 -04:00
|
|
|
main(int argc, char *argv[]) {
|
2001-07-26 17:54:43 -04:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
XML_Parser parser = XML_ParserCreate(NULL);
|
|
|
|
int done;
|
|
|
|
int depth = 0;
|
2016-05-07 10:45:58 -04:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
2001-07-26 17:54:43 -04:00
|
|
|
XML_SetUserData(parser, &depth);
|
|
|
|
XML_SetElementHandler(parser, startElement, endElement);
|
|
|
|
do {
|
2016-03-11 20:59:53 -05:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), stdin);
|
2001-07-26 17:54:43 -04:00
|
|
|
done = len < sizeof(buf);
|
2017-12-03 12:11:00 -05:00
|
|
|
if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) {
|
2019-08-03 14:34:54 -04:00
|
|
|
fprintf(stderr, "%" XML_FMT_STR " at line %" XML_FMT_INT_MOD "u\n",
|
2002-07-01 10:45:51 -04:00
|
|
|
XML_ErrorString(XML_GetErrorCode(parser)),
|
|
|
|
XML_GetCurrentLineNumber(parser));
|
2019-09-05 04:17:51 -04:00
|
|
|
XML_ParserFree(parser);
|
2001-07-26 17:54:43 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
} while (! done);
|
2001-07-26 17:54:43 -04:00
|
|
|
XML_ParserFree(parser);
|
|
|
|
return 0;
|
|
|
|
}
|