xmlstream.c 4.8 KB

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