Demonstrates the use of the spxml library to read and parse streamed XML (via HTTP). It calls out to the google weather API, parses the resulting XML and prints out the current temperature (for Los Angeles). It makes use of the HTTPClient library.

Dependencies:   mbed spxml

Committer:
hlipka
Date:
Wed Nov 24 21:23:15 2010 +0000
Revision:
0:ee591985c885
first public version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 0:ee591985c885 1 #include "mbed.h"
hlipka 0:ee591985c885 2
hlipka 0:ee591985c885 3 #include "EthernetNetIf.h"
hlipka 0:ee591985c885 4 #include "HTTPClient.h"
hlipka 0:ee591985c885 5
hlipka 0:ee591985c885 6 #include "spdomparser.hpp"
hlipka 0:ee591985c885 7 #include "spxmlnode.hpp"
hlipka 0:ee591985c885 8 #include "spxmlhandle.hpp"
hlipka 0:ee591985c885 9
hlipka 0:ee591985c885 10 EthernetNetIf eth;
hlipka 0:ee591985c885 11 HTTPClient http;
hlipka 0:ee591985c885 12
hlipka 0:ee591985c885 13 HTTPResult result;
hlipka 0:ee591985c885 14 bool completed = false;
hlipka 0:ee591985c885 15 void request_callback(HTTPResult r) {
hlipka 0:ee591985c885 16 result = r;
hlipka 0:ee591985c885 17 completed = true;
hlipka 0:ee591985c885 18 }
hlipka 0:ee591985c885 19
hlipka 0:ee591985c885 20 void parseWeather(SP_XmlElementNode *node) {
hlipka 0:ee591985c885 21 SP_XmlHandle handle(node);
hlipka 0:ee591985c885 22 SP_XmlElementNode * tempc = handle.getChild( "temp_c" ).toElement();
hlipka 0:ee591985c885 23 if (tempc) {
hlipka 0:ee591985c885 24 printf("current temp=%sC\n",tempc->getAttrValue("data"));
hlipka 0:ee591985c885 25 }
hlipka 0:ee591985c885 26 SP_XmlElementNode * tempf = handle.getChild( "temp_f" ).toElement();
hlipka 0:ee591985c885 27 if (tempf) {
hlipka 0:ee591985c885 28 printf("current temp=%sF\n",tempf->getAttrValue("data"));
hlipka 0:ee591985c885 29 }
hlipka 0:ee591985c885 30 }
hlipka 0:ee591985c885 31
hlipka 0:ee591985c885 32 int main() {
hlipka 0:ee591985c885 33 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
hlipka 0:ee591985c885 34 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
hlipka 0:ee591985c885 35
hlipka 0:ee591985c885 36 printf("setup\n");
hlipka 0:ee591985c885 37 EthernetErr ethErr = eth.setup();
hlipka 0:ee591985c885 38 if (ethErr) {
hlipka 0:ee591985c885 39 printf("Error in setup\n");
hlipka 0:ee591985c885 40 return -1;
hlipka 0:ee591985c885 41 }
hlipka 0:ee591985c885 42 printf("setup ok\n");
hlipka 0:ee591985c885 43
hlipka 0:ee591985c885 44 SP_XmlDomParser parser;
hlipka 0:ee591985c885 45
hlipka 0:ee591985c885 46 HTTPStream stream;
hlipka 0:ee591985c885 47
hlipka 0:ee591985c885 48 char BigBuf[512 + 1] = {0};
hlipka 0:ee591985c885 49 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
hlipka 0:ee591985c885 50
hlipka 0:ee591985c885 51 HTTPResult r = http.get("http://www.google.com/ig/api?weather=Los+Angeles", &stream, request_callback);
hlipka 0:ee591985c885 52
hlipka 0:ee591985c885 53 while (!completed) {
hlipka 0:ee591985c885 54 Net::poll(); //Polls the Networking stack
hlipka 0:ee591985c885 55 if (stream.readable()) {
hlipka 0:ee591985c885 56 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
hlipka 0:ee591985c885 57
hlipka 0:ee591985c885 58 parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
hlipka 0:ee591985c885 59
hlipka 0:ee591985c885 60 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
hlipka 0:ee591985c885 61 }
hlipka 0:ee591985c885 62 }
hlipka 0:ee591985c885 63 if (result == HTTP_OK) {
hlipka 0:ee591985c885 64 printf("Read completely\n");
hlipka 0:ee591985c885 65 } else {
hlipka 0:ee591985c885 66 printf("Error %d\n", result);
hlipka 0:ee591985c885 67 return -1;
hlipka 0:ee591985c885 68 }
hlipka 0:ee591985c885 69
hlipka 0:ee591985c885 70 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
hlipka 0:ee591985c885 71 SP_XmlElementNode * child2 = rootHandle.getChild( "weather" )
hlipka 0:ee591985c885 72 .getChild( "current_conditions").toElement();
hlipka 0:ee591985c885 73
hlipka 0:ee591985c885 74 if ( child2 ) {
hlipka 0:ee591985c885 75 parseWeather(child2);
hlipka 0:ee591985c885 76 }
hlipka 0:ee591985c885 77
hlipka 0:ee591985c885 78 if ( NULL != parser.getError() ) {
hlipka 0:ee591985c885 79 printf( "\n\nerror: %s\n", parser.getError() );
hlipka 0:ee591985c885 80 }
hlipka 0:ee591985c885 81
hlipka 0:ee591985c885 82 printf("end\n");
hlipka 0:ee591985c885 83
hlipka 0:ee591985c885 84 }