2
0

xmlstream.c 4.9 KB

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