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
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #include <expat.h>
37 #include <curl/curl.h>
38
39 struct MemoryStruct {
40 char *memory;
41 size_t size;
42 };
43
44 struct ParserStruct {
45 int ok;
46 size_t tags;
47 size_t depth;
48 struct MemoryStruct characters;
49 };
50
startElement(void * userData,const XML_Char * name,const XML_Char ** atts)51 static void startElement(void *userData, const XML_Char *name, const XML_Char **atts)
52 {
53 struct ParserStruct *state = (struct ParserStruct *) userData;
54 state->tags++;
55 state->depth++;
56
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;
61 }
62
characterDataHandler(void * userData,const XML_Char * s,int len)63 static void characterDataHandler(void *userData, const XML_Char *s, int len)
64 {
65 struct ParserStruct *state = (struct ParserStruct *) userData;
66 struct MemoryStruct *mem = &state->characters;
67
68 mem->memory = realloc(mem->memory, mem->size + len + 1);
69 if(mem->memory == NULL) {
70 /* Out of memory. */
71 fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
72 state->ok = 0;
73 return;
74 }
75
76 memcpy(&(mem->memory[mem->size]), s, len);
77 mem->size += len;
78 mem->memory[mem->size] = 0;
79 }
80
endElement(void * userData,const XML_Char * name)81 static void endElement(void *userData, const XML_Char *name)
82 {
83 struct ParserStruct *state = (struct ParserStruct *) userData;
84 state->depth--;
85
86 printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
87 }
88
parseStreamCallback(void * contents,size_t length,size_t nmemb,void * userp)89 static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb, void *userp)
90 {
91 XML_Parser parser = (XML_Parser) userp;
92 size_t real_size = length * nmemb;
93 struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
94
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));
100 state->ok = 0;
101 }
102
103 return real_size;
104 }
105
main(void)106 int main(void)
107 {
108 CURL *curl_handle;
109 CURLcode res;
110 XML_Parser parser;
111 struct ParserStruct state;
112
113 /* Initialize the state structure for parsing. */
114 memset(&state, 0, sizeof(struct ParserStruct));
115 state.ok = 1;
116
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);
122
123 /* Initialize 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);
129
130 printf("Depth Characters Closing Tag\n");
131
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));
137 }
138 else if (state.ok) {
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));
144 }
145 else {
146 printf(" --------------\n");
147 printf(" %lu tags total\n", state.tags);
148 }
149 }
150
151 /* Clean up. */
152 free(state.characters.memory);
153 XML_ParserFree(parser);
154 curl_easy_cleanup(curl_handle);
155 curl_global_cleanup();
156
157 return 0;
158 }
159