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