2013-05-19 05:14:01 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-01-04 09:34:05 -05:00
|
|
|
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2013-05-19 05:14:01 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2013-05-19 05:14:01 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2016-01-04 09:34:05 -05:00
|
|
|
/* <DESC>
|
|
|
|
* Stream-parse a document using the streaming Expat parser.
|
|
|
|
* </DESC>
|
|
|
|
*/
|
|
|
|
/* Written by David Strauss
|
2013-05-19 05:14:01 -04:00
|
|
|
*
|
|
|
|
* Expat => http://www.libexpat.org/
|
|
|
|
*
|
|
|
|
* gcc -Wall -I/usr/local/include xmlstream.c -lcurl -lexpat -o xmlstream
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <expat.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
struct MemoryStruct {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ParserStruct {
|
|
|
|
int ok;
|
|
|
|
size_t tags;
|
|
|
|
size_t depth;
|
|
|
|
struct MemoryStruct characters;
|
|
|
|
};
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
static void startElement(void *userData, const XML_Char *name,
|
|
|
|
const XML_Char **atts)
|
2013-05-19 05:14:01 -04:00
|
|
|
{
|
|
|
|
struct ParserStruct *state = (struct ParserStruct *) userData;
|
|
|
|
state->tags++;
|
|
|
|
state->depth++;
|
|
|
|
|
|
|
|
/* Get a clean slate for reading in character data. */
|
|
|
|
free(state->characters.memory);
|
|
|
|
state->characters.memory = NULL;
|
|
|
|
state->characters.size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void characterDataHandler(void *userData, const XML_Char *s, int len)
|
|
|
|
{
|
|
|
|
struct ParserStruct *state = (struct ParserStruct *) userData;
|
|
|
|
struct MemoryStruct *mem = &state->characters;
|
|
|
|
|
|
|
|
mem->memory = realloc(mem->memory, mem->size + len + 1);
|
|
|
|
if(mem->memory == NULL) {
|
|
|
|
/* Out of memory. */
|
|
|
|
fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
|
|
|
|
state->ok = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&(mem->memory[mem->size]), s, len);
|
|
|
|
mem->size += len;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void endElement(void *userData, const XML_Char *name)
|
|
|
|
{
|
|
|
|
struct ParserStruct *state = (struct ParserStruct *) userData;
|
|
|
|
state->depth--;
|
|
|
|
|
|
|
|
printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb,
|
|
|
|
void *userp)
|
2013-05-19 05:14:01 -04:00
|
|
|
{
|
|
|
|
XML_Parser parser = (XML_Parser) userp;
|
|
|
|
size_t real_size = length * nmemb;
|
|
|
|
struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
|
|
|
|
|
|
|
|
/* Only parse if we're not already in a failure state. */
|
2016-02-11 03:42:38 -05:00
|
|
|
if(state->ok && XML_Parse(parser, contents, real_size, 0) == 0) {
|
2013-05-19 05:14:01 -04:00
|
|
|
int error_code = XML_GetErrorCode(parser);
|
2016-02-11 03:42:38 -05:00
|
|
|
fprintf(stderr, "Parsing response buffer of length %lu failed"
|
|
|
|
" with error code %d (%s).\n",
|
2013-05-19 05:14:01 -04:00
|
|
|
real_size, error_code, XML_ErrorString(error_code));
|
|
|
|
state->ok = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return real_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
CURL *curl_handle;
|
|
|
|
CURLcode res;
|
|
|
|
XML_Parser parser;
|
|
|
|
struct ParserStruct state;
|
|
|
|
|
|
|
|
/* Initialize the state structure for parsing. */
|
|
|
|
memset(&state, 0, sizeof(struct ParserStruct));
|
|
|
|
state.ok = 1;
|
|
|
|
|
|
|
|
/* Initialize a namespace-aware parser. */
|
|
|
|
parser = XML_ParserCreateNS(NULL, '\0');
|
|
|
|
XML_SetUserData(parser, &state);
|
|
|
|
XML_SetElementHandler(parser, startElement, endElement);
|
|
|
|
XML_SetCharacterDataHandler(parser, characterDataHandler);
|
|
|
|
|
2015-06-08 07:22:54 -04:00
|
|
|
/* Initialize a libcurl handle. */
|
2013-05-19 05:14:01 -04:00
|
|
|
curl_global_init(CURL_GLOBAL_ALL ^ CURL_GLOBAL_SSL);
|
|
|
|
curl_handle = curl_easy_init();
|
2016-02-11 03:42:38 -05:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_URL,
|
|
|
|
"http://www.w3schools.com/xml/simple.xml");
|
2013-05-19 05:14:01 -04:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
|
|
|
|
|
|
|
|
printf("Depth Characters Closing Tag\n");
|
|
|
|
|
|
|
|
/* Perform the request and any follow-up parsing. */
|
|
|
|
res = curl_easy_perform(curl_handle);
|
|
|
|
if(res != CURLE_OK) {
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
|
|
|
}
|
2016-02-11 03:42:38 -05:00
|
|
|
else if(state.ok) {
|
2013-05-19 05:14:01 -04:00
|
|
|
/* Expat requires one final call to finalize parsing. */
|
2016-02-11 03:42:38 -05:00
|
|
|
if(XML_Parse(parser, NULL, 0, 1) == 0) {
|
2013-05-19 05:14:01 -04:00
|
|
|
int error_code = XML_GetErrorCode(parser);
|
|
|
|
fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
|
|
|
|
error_code, XML_ErrorString(error_code));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(" --------------\n");
|
|
|
|
printf(" %lu tags total\n", state.tags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up. */
|
|
|
|
free(state.characters.memory);
|
|
|
|
XML_ParserFree(parser);
|
|
|
|
curl_easy_cleanup(curl_handle);
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|