Small demo of TinyXml. TinyXml is a compact library that implements XML Parsing and Manipulation.

Dependencies:   mbed TINYXML

Committer:
wvd_vegt
Date:
Mon Apr 18 18:55:17 2011 +0000
Revision:
0:091ca51848a0
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wvd_vegt 0:091ca51848a0 1 #include "mbed.h"
wvd_vegt 0:091ca51848a0 2 #include "tinyxml.h"
wvd_vegt 0:091ca51848a0 3
wvd_vegt 0:091ca51848a0 4 #include <string>
wvd_vegt 0:091ca51848a0 5
wvd_vegt 0:091ca51848a0 6 //See http://www.grinninglizard.com/tinyxml/ for more information on how to use tinyxml.
wvd_vegt 0:091ca51848a0 7 //See TINYXML Library for license.
wvd_vegt 0:091ca51848a0 8 //
wvd_vegt 0:091ca51848a0 9 //TODO Document library and app in embed style.
wvd_vegt 0:091ca51848a0 10
wvd_vegt 0:091ca51848a0 11 DigitalOut myled(LED1);
wvd_vegt 0:091ca51848a0 12
wvd_vegt 0:091ca51848a0 13 int main() {
wvd_vegt 0:091ca51848a0 14
wvd_vegt 0:091ca51848a0 15 //Setup Tx, Rx.
wvd_vegt 0:091ca51848a0 16 Serial usb(USBTX, USBRX);
wvd_vegt 0:091ca51848a0 17 usb.baud(115200);
wvd_vegt 0:091ca51848a0 18
wvd_vegt 0:091ca51848a0 19 // Create the local filesystem under the name "local".
wvd_vegt 0:091ca51848a0 20 // DO NOT FORGET THIS CALL OR SAVING WON'T WORK!
wvd_vegt 0:091ca51848a0 21 LocalFileSystem local("local");
wvd_vegt 0:091ca51848a0 22
wvd_vegt 0:091ca51848a0 23 // Create some Xml to experiment on.
wvd_vegt 0:091ca51848a0 24 const char* demoStart =
wvd_vegt 0:091ca51848a0 25 "<?xml version=\"1.0\" standalone='no' >\r\n"
wvd_vegt 0:091ca51848a0 26 "<!-- Our to do list data -->\r\n"
wvd_vegt 0:091ca51848a0 27 "<ToDo>\r\n"
wvd_vegt 0:091ca51848a0 28 "<!-- Do I need a secure PDA? -->\r\n"
wvd_vegt 0:091ca51848a0 29 "<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>\r\n"
wvd_vegt 0:091ca51848a0 30 "<Item priority=\"2\" distance='none'> Do bills </Item>\r\n"
wvd_vegt 0:091ca51848a0 31 "<Item priority=\"2\" distance='far &amp; back'> Look for Evil Dinosaurs! </Item>\r\n"
wvd_vegt 0:091ca51848a0 32 "</ToDo>\r\n";
wvd_vegt 0:091ca51848a0 33
wvd_vegt 0:091ca51848a0 34 // Load Xml into a XmlDocument.
wvd_vegt 0:091ca51848a0 35 TiXmlDocument doc("/local/demotest.xml");
wvd_vegt 0:091ca51848a0 36
wvd_vegt 0:091ca51848a0 37 // Parse Xml.
wvd_vegt 0:091ca51848a0 38 doc.Parse(demoStart);
wvd_vegt 0:091ca51848a0 39
wvd_vegt 0:091ca51848a0 40 // Get the tagname of the Root Element (ToDo).
wvd_vegt 0:091ca51848a0 41 // Note: doc.RootElement()->Value().c_str() is not compatible with printf! E153
wvd_vegt 0:091ca51848a0 42 string tag = doc.RootElement()->Value();
wvd_vegt 0:091ca51848a0 43 usb.printf("RootElement = <%s>\r\n\r\n", tag.c_str());
wvd_vegt 0:091ca51848a0 44
wvd_vegt 0:091ca51848a0 45 // Iterate Elements with for loop.
wvd_vegt 0:091ca51848a0 46 usb.printf("Enumerating Item Child Tags:\r\n");
wvd_vegt 0:091ca51848a0 47 for (TiXmlNode* child = doc.RootElement()->FirstChild(); child; child = child->NextSibling() ) {
wvd_vegt 0:091ca51848a0 48 if (child->Type()==TiXmlNode::TINYXML_ELEMENT) {
wvd_vegt 0:091ca51848a0 49 tag = child->Value();
wvd_vegt 0:091ca51848a0 50 usb.printf(" Child = <%s>\r\n", tag.c_str());
wvd_vegt 0:091ca51848a0 51 usb.printf(" Text = '%s'\r\n\r\n", child->ToElement()->GetText());
wvd_vegt 0:091ca51848a0 52 }
wvd_vegt 0:091ca51848a0 53 }
wvd_vegt 0:091ca51848a0 54
wvd_vegt 0:091ca51848a0 55 //Walk Elements by FirstChildElement/NextSiblingElement.
wvd_vegt 0:091ca51848a0 56 usb.printf("Walking Item Child Tags:\r\n");
wvd_vegt 0:091ca51848a0 57 TiXmlElement* child = doc.RootElement()->FirstChildElement("Item");
wvd_vegt 0:091ca51848a0 58
wvd_vegt 0:091ca51848a0 59 do {
wvd_vegt 0:091ca51848a0 60 tag = child->Value();
wvd_vegt 0:091ca51848a0 61 usb.printf(" Child = <%s>\r\n", tag.c_str());
wvd_vegt 0:091ca51848a0 62 usb.printf(" Text = '%s'\r\n\r\n", child->ToElement()->GetText());
wvd_vegt 0:091ca51848a0 63
wvd_vegt 0:091ca51848a0 64 child = child->NextSiblingElement("Item");
wvd_vegt 0:091ca51848a0 65 } while (child);
wvd_vegt 0:091ca51848a0 66
wvd_vegt 0:091ca51848a0 67 //Save to a file on the /local/ filesystem on Usb.
wvd_vegt 0:091ca51848a0 68 usb.printf("Saving with filename:\r\n");
wvd_vegt 0:091ca51848a0 69 doc.SaveFile("/local/out_1.xml");
wvd_vegt 0:091ca51848a0 70
wvd_vegt 0:091ca51848a0 71 //Save to a file on the /local/ filesystem on Usb.
wvd_vegt 0:091ca51848a0 72 usb.printf("Saving with FILE:\r\n");
wvd_vegt 0:091ca51848a0 73 FILE *fp = fopen("/local/out_2.xml", "w");
wvd_vegt 0:091ca51848a0 74 doc.SaveFile(fp);
wvd_vegt 0:091ca51848a0 75 fclose(fp);
wvd_vegt 0:091ca51848a0 76
wvd_vegt 0:091ca51848a0 77 if ( doc.Error() ) {
wvd_vegt 0:091ca51848a0 78 usb.printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
wvd_vegt 0:091ca51848a0 79
wvd_vegt 0:091ca51848a0 80 exit( 1 );
wvd_vegt 0:091ca51848a0 81 }
wvd_vegt 0:091ca51848a0 82
wvd_vegt 0:091ca51848a0 83 //Print the Parsed Xml.
wvd_vegt 0:091ca51848a0 84 usb.printf("Printing XML:\r\n");
wvd_vegt 0:091ca51848a0 85 TiXmlPrinter printer;
wvd_vegt 0:091ca51848a0 86 printer.SetLineBreak("\r\n"); //Default is "\n".
wvd_vegt 0:091ca51848a0 87 doc.Accept(&printer);
wvd_vegt 0:091ca51848a0 88
wvd_vegt 0:091ca51848a0 89 usb.printf("%s\r\n", printer.CStr());
wvd_vegt 0:091ca51848a0 90
wvd_vegt 0:091ca51848a0 91 while (1) {
wvd_vegt 0:091ca51848a0 92 //Wait...
wvd_vegt 0:091ca51848a0 93 }
wvd_vegt 0:091ca51848a0 94 }