1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Stream-parse a document using the streaming Expat parser.
26 /* Written by David Strauss
28 * Expat => http://www.libexpat.org/
30 * gcc -Wall -I/usr/local/include xmlstream.c -lcurl -lexpat -o xmlstream
40 #include <curl/curl.h>
51 struct MemoryStruct characters;
54 static void startElement(void *userData, const XML_Char *name,
55 const XML_Char **atts)
57 struct ParserStruct *state = (struct ParserStruct *) userData;
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;
67 static void characterDataHandler(void *userData, const XML_Char *s, int len)
69 struct ParserStruct *state = (struct ParserStruct *) userData;
70 struct MemoryStruct *mem = &state->characters;
72 mem->memory = realloc(mem->memory, mem->size + len + 1);
73 if(mem->memory == NULL) {
75 fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
80 memcpy(&(mem->memory[mem->size]), s, len);
82 mem->memory[mem->size] = 0;
85 static void endElement(void *userData, const XML_Char *name)
87 struct ParserStruct *state = (struct ParserStruct *) userData;
90 printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
93 static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb,
96 XML_Parser parser = (XML_Parser) userp;
97 size_t real_size = length * nmemb;
98 struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
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));
117 struct ParserStruct state;
119 /* Initialize the state structure for parsing. */
120 memset(&state, 0, sizeof(struct ParserStruct));
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);
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);
137 printf("Depth Characters Closing Tag\n");
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));
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));
153 printf(" --------------\n");
154 printf(" %lu tags total\n", state.tags);
159 free(state.characters.memory);
160 XML_ParserFree(parser);
161 curl_easy_cleanup(curl_handle);
162 curl_global_cleanup();