2
0

xmlstream.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Stream-parse a document using the streaming Expat parser.
  26. * </DESC>
  27. */
  28. /* Written by David Strauss
  29. *
  30. * Expat => https://libexpat.github.io/
  31. *
  32. * gcc -Wall -I/usr/local/include xmlstream.c -lcurl -lexpat -o xmlstream
  33. *
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <expat.h>
  39. #include <curl/curl.h>
  40. struct MemoryStruct {
  41. char *memory;
  42. size_t size;
  43. };
  44. struct ParserStruct {
  45. int ok;
  46. size_t tags;
  47. size_t depth;
  48. struct MemoryStruct characters;
  49. };
  50. static void startElement(void *userData, const XML_Char *name,
  51. const XML_Char **atts)
  52. {
  53. struct ParserStruct *state = (struct ParserStruct *) userData;
  54. state->tags++;
  55. state->depth++;
  56. /* Get a clean slate for reading in character data. */
  57. free(state->characters.memory);
  58. state->characters.memory = NULL;
  59. state->characters.size = 0;
  60. }
  61. static void characterDataHandler(void *userData, const XML_Char *s, int len)
  62. {
  63. struct ParserStruct *state = (struct ParserStruct *) userData;
  64. struct MemoryStruct *mem = &state->characters;
  65. char *ptr = realloc(mem->memory, mem->size + len + 1);
  66. if(!ptr) {
  67. /* Out of memory. */
  68. fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
  69. state->ok = 0;
  70. return;
  71. }
  72. mem->memory = ptr;
  73. memcpy(&(mem->memory[mem->size]), s, len);
  74. mem->size += len;
  75. mem->memory[mem->size] = 0;
  76. }
  77. static void endElement(void *userData, const XML_Char *name)
  78. {
  79. struct ParserStruct *state = (struct ParserStruct *) userData;
  80. state->depth--;
  81. printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
  82. }
  83. static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb,
  84. void *userp)
  85. {
  86. XML_Parser parser = (XML_Parser) userp;
  87. size_t real_size = length * nmemb;
  88. struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
  89. /* Only parse if we are not already in a failure state. */
  90. if(state->ok && XML_Parse(parser, contents, real_size, 0) == 0) {
  91. int error_code = XML_GetErrorCode(parser);
  92. fprintf(stderr, "Parsing response buffer of length %lu failed"
  93. " with error code %d (%s).\n",
  94. real_size, error_code, XML_ErrorString(error_code));
  95. state->ok = 0;
  96. }
  97. return real_size;
  98. }
  99. int main(void)
  100. {
  101. CURL *curl_handle;
  102. CURLcode res;
  103. XML_Parser parser;
  104. struct ParserStruct state;
  105. /* Initialize the state structure for parsing. */
  106. memset(&state, 0, sizeof(struct ParserStruct));
  107. state.ok = 1;
  108. /* Initialize a namespace-aware parser. */
  109. parser = XML_ParserCreateNS(NULL, '\0');
  110. XML_SetUserData(parser, &state);
  111. XML_SetElementHandler(parser, startElement, endElement);
  112. XML_SetCharacterDataHandler(parser, characterDataHandler);
  113. /* Initialize a libcurl handle. */
  114. curl_global_init(CURL_GLOBAL_DEFAULT);
  115. curl_handle = curl_easy_init();
  116. curl_easy_setopt(curl_handle, CURLOPT_URL,
  117. "https://www.w3schools.com/xml/simple.xml");
  118. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
  119. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
  120. printf("Depth Characters Closing Tag\n");
  121. /* Perform the request and any follow-up parsing. */
  122. res = curl_easy_perform(curl_handle);
  123. if(res != CURLE_OK) {
  124. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  125. curl_easy_strerror(res));
  126. }
  127. else if(state.ok) {
  128. /* Expat requires one final call to finalize parsing. */
  129. if(XML_Parse(parser, NULL, 0, 1) == 0) {
  130. int error_code = XML_GetErrorCode(parser);
  131. fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
  132. error_code, XML_ErrorString(error_code));
  133. }
  134. else {
  135. printf(" --------------\n");
  136. printf(" %lu tags total\n", state.tags);
  137. }
  138. }
  139. /* Clean up. */
  140. free(state.characters.memory);
  141. XML_ParserFree(parser);
  142. curl_easy_cleanup(curl_handle);
  143. curl_global_cleanup();
  144. return 0;
  145. }