xmlstream.c 5.0 KB

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