1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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 ***************************************************************************/
22 /* Stream-parse a document using the streaming Expat parser.
23 * Written by David Strauss
25 * Expat => http://www.libexpat.org/
27 * gcc -Wall -I/usr/local/include xmlstream.c -lcurl -lexpat -o xmlstream
37 #include <curl/curl.h>
48 struct MemoryStruct characters;
51 static void startElement(void *userData, const XML_Char *name, const XML_Char **atts)
53 struct ParserStruct *state = (struct ParserStruct *) userData;
57 /* Get a clean slate for reading in character data. */
58 free(state->characters.memory);
59 state->characters.memory = NULL;
60 state->characters.size = 0;
63 static void characterDataHandler(void *userData, const XML_Char *s, int len)
65 struct ParserStruct *state = (struct ParserStruct *) userData;
66 struct MemoryStruct *mem = &state->characters;
68 mem->memory = realloc(mem->memory, mem->size + len + 1);
69 if(mem->memory == NULL) {
71 fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
76 memcpy(&(mem->memory[mem->size]), s, len);
78 mem->memory[mem->size] = 0;
81 static void endElement(void *userData, const XML_Char *name)
83 struct ParserStruct *state = (struct ParserStruct *) userData;
86 printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
89 static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb, void *userp)
91 XML_Parser parser = (XML_Parser) userp;
92 size_t real_size = length * nmemb;
93 struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
95 /* Only parse if we're not already in a failure state. */
96 if (state->ok && XML_Parse(parser, contents, real_size, 0) == 0) {
97 int error_code = XML_GetErrorCode(parser);
98 fprintf(stderr, "Parsing response buffer of length %lu failed with error code %d (%s).\n",
99 real_size, error_code, XML_ErrorString(error_code));
111 struct ParserStruct state;
113 /* Initialize the state structure for parsing. */
114 memset(&state, 0, sizeof(struct ParserStruct));
117 /* Initialize a namespace-aware parser. */
118 parser = XML_ParserCreateNS(NULL, '\0');
119 XML_SetUserData(parser, &state);
120 XML_SetElementHandler(parser, startElement, endElement);
121 XML_SetCharacterDataHandler(parser, characterDataHandler);
123 /* Initalize a libcurl handle. */
124 curl_global_init(CURL_GLOBAL_ALL ^ CURL_GLOBAL_SSL);
125 curl_handle = curl_easy_init();
126 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.w3schools.com/xml/simple.xml");
127 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
128 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
130 printf("Depth Characters Closing Tag\n");
132 /* Perform the request and any follow-up parsing. */
133 res = curl_easy_perform(curl_handle);
134 if(res != CURLE_OK) {
135 fprintf(stderr, "curl_easy_perform() failed: %s\n",
136 curl_easy_strerror(res));
139 /* Expat requires one final call to finalize parsing. */
140 if (XML_Parse(parser, NULL, 0, 1) == 0) {
141 int error_code = XML_GetErrorCode(parser);
142 fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
143 error_code, XML_ErrorString(error_code));
146 printf(" --------------\n");
147 printf(" %lu tags total\n", state.tags);
152 free(state.characters.memory);
153 XML_ParserFree(parser);
154 curl_easy_cleanup(curl_handle);
155 curl_global_cleanup();