elements.c 3.2 KB

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