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

Files at this revision

API Documentation at this revision

Comitter:
hlipka
Date:
Wed Nov 24 21:23:15 2010 +0000
Commit message:
first public version

Changed in this revision

HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
NetServices.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
spxml.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Wed Nov 24 21:23:15 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetServices.lib	Wed Nov 24 21:23:15 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/segundo/code/NetServices/#4e2468d7d5cb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 24 21:23:15 2010 +0000
@@ -0,0 +1,84 @@
+#include "mbed.h"
+
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+
+#include "spdomparser.hpp"
+#include "spxmlnode.hpp"
+#include "spxmlhandle.hpp"
+
+EthernetNetIf eth;
+HTTPClient http;
+
+HTTPResult result;
+bool completed = false;
+void request_callback(HTTPResult r) {
+    result = r;
+    completed = true;
+}
+
+void parseWeather(SP_XmlElementNode *node) {
+    SP_XmlHandle handle(node);
+    SP_XmlElementNode * tempc = handle.getChild( "temp_c" ).toElement();
+    if (tempc) {
+        printf("current temp=%sC\n",tempc->getAttrValue("data"));
+    }
+    SP_XmlElementNode * tempf = handle.getChild( "temp_f" ).toElement();
+    if (tempf) {
+        printf("current temp=%sF\n",tempf->getAttrValue("data"));
+    }
+}
+
+int main() {
+    // the eth and HTTP code has be taken directly from the HTTPStream documentation page
+    // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
+
+    printf("setup\n");
+    EthernetErr ethErr = eth.setup();
+    if (ethErr) {
+        printf("Error in setup\n");
+        return -1;
+    }
+    printf("setup ok\n");
+
+    SP_XmlDomParser parser;
+
+    HTTPStream stream;
+
+    char BigBuf[512 + 1] = {0};
+    stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
+
+    HTTPResult r = http.get("http://www.google.com/ig/api?weather=Los+Angeles", &stream, request_callback);
+
+    while (!completed) {
+        Net::poll(); //Polls the Networking stack
+        if (stream.readable()) {
+            BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
+            
+            parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
+            
+            stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
+        }
+    }
+    if (result == HTTP_OK) {
+        printf("Read completely\n");
+    } else {
+        printf("Error %d\n", result);
+        return -1;
+    }
+
+    SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
+    SP_XmlElementNode * child2 = rootHandle.getChild( "weather" )
+                                 .getChild( "current_conditions").toElement();
+
+    if ( child2 ) {
+        parseWeather(child2);
+    }
+
+    if ( NULL != parser.getError() ) {
+        printf( "\n\nerror: %s\n", parser.getError() );
+    }
+
+    printf("end\n");
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Nov 24 21:23:15 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spxml.lib	Wed Nov 24 21:23:15 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hlipka/code/spxml/#3fa97f2c0505