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