2
0

xmlstream.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. (void)name;
  57. (void)atts;
  58. /* Get a clean slate for reading in character data. */
  59. free(state->characters.memory);
  60. state->characters.memory = NULL;
  61. state->characters.size = 0;
  62. }
  63. static void characterDataHandler(void *userData, const XML_Char *s, int len)
  64. {
  65. struct ParserStruct *state = (struct ParserStruct *) userData;
  66. struct MemoryStruct *mem = &state->characters;
  67. char *ptr = realloc(mem->memory, mem->size + (unsigned long)len + 1);
  68. if(!ptr) {
  69. /* Out of memory. */
  70. fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
  71. state->ok = 0;
  72. return;
  73. }
  74. mem->memory = ptr;
  75. memcpy(&(mem->memory[mem->size]), s, len);
  76. mem->size += (unsigned long)len;
  77. mem->memory[mem->size] = 0;
  78. }
  79. static void endElement(void *userData, const XML_Char *name)
  80. {
  81. struct ParserStruct *state = (struct ParserStruct *) userData;
  82. state->depth--;
  83. printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
  84. }
  85. static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb,
  86. void *userp)
  87. {
  88. XML_Parser parser = (XML_Parser) userp;
  89. size_t real_size = length * nmemb;
  90. struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
  91. /* Only parse if we are not already in a failure state. */
  92. if(state->ok && XML_Parse(parser, contents, (int)real_size, 0) == 0) {
  93. enum XML_Error error_code = XML_GetErrorCode(parser);
  94. fprintf(stderr, "Parsing response buffer of length %lu failed"
  95. " with error code %d (%s).\n",
  96. real_size, error_code, XML_ErrorString(error_code));
  97. state->ok = 0;
  98. }
  99. return real_size;
  100. }
  101. int main(void)
  102. {
  103. CURL *curl_handle;
  104. CURLcode res;
  105. XML_Parser parser;
  106. struct ParserStruct state;
  107. /* Initialize the state structure for parsing. */
  108. memset(&state, 0, sizeof(struct ParserStruct));
  109. state.ok = 1;
  110. /* Initialize a namespace-aware parser. */
  111. parser = XML_ParserCreateNS(NULL, '\0');
  112. XML_SetUserData(parser, &state);
  113. XML_SetElementHandler(parser, startElement, endElement);
  114. XML_SetCharacterDataHandler(parser, characterDataHandler);
  115. /* Initialize a libcurl handle. */
  116. curl_global_init(CURL_GLOBAL_DEFAULT);
  117. curl_handle = curl_easy_init();
  118. curl_easy_setopt(curl_handle, CURLOPT_URL,
  119. "https://www.w3schools.com/xml/simple.xml");
  120. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
  121. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
  122. printf("Depth Characters Closing Tag\n");
  123. /* Perform the request and any follow-up parsing. */
  124. res = curl_easy_perform(curl_handle);
  125. if(res != CURLE_OK) {
  126. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  127. curl_easy_strerror(res));
  128. }
  129. else if(state.ok) {
  130. /* Expat requires one final call to finalize parsing. */
  131. if(XML_Parse(parser, NULL, 0, 1) == 0) {
  132. enum XML_Error error_code = XML_GetErrorCode(parser);
  133. fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
  134. error_code, XML_ErrorString(error_code));
  135. }
  136. else {
  137. printf(" --------------\n");
  138. printf(" %lu tags total\n", state.tags);
  139. }
  140. }
  141. /* Clean up. */
  142. free(state.characters.memory);
  143. XML_ParserFree(parser);
  144. curl_easy_cleanup(curl_handle);
  145. curl_global_cleanup();
  146. return 0;
  147. }