elements.c 3.5 KB

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