Wim van der Vegt / Mbed 2 deprecated tinyxml_test

Dependencies:   mbed TINYXML

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "tinyxml.h"
00003 
00004 #include <string>
00005 
00006 //See http://www.grinninglizard.com/tinyxml/ for more information on how to use tinyxml.
00007 //See TINYXML Library for license.
00008 //
00009 //TODO Document library and app in embed style.
00010 
00011 DigitalOut myled(LED1);
00012 
00013 int main() {
00014 
00015     //Setup Tx, Rx.
00016     Serial usb(USBTX, USBRX);
00017     usb.baud(115200);
00018 
00019     // Create the local filesystem under the name "local". 
00020     // DO NOT FORGET THIS CALL OR SAVING WON'T WORK!
00021     LocalFileSystem local("local");
00022 
00023     // Create some Xml to experiment on.
00024     const char* demoStart =
00025         "<?xml version=\"1.0\"  standalone='no' >\r\n"
00026         "<!-- Our to do list data -->\r\n"
00027         "<ToDo>\r\n"
00028         "<!-- Do I need a secure PDA? -->\r\n"
00029         "<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>\r\n"
00030         "<Item priority=\"2\" distance='none'> Do bills   </Item>\r\n"
00031         "<Item priority=\"2\" distance='far &amp; back'> Look for Evil Dinosaurs! </Item>\r\n"
00032         "</ToDo>\r\n";
00033 
00034     // Load Xml into a XmlDocument.
00035     TiXmlDocument doc("/local/demotest.xml");
00036 
00037     // Parse Xml.
00038     doc.Parse(demoStart);
00039 
00040     // Get the tagname of the Root Element (ToDo).
00041     // Note: doc.RootElement()->Value().c_str() is not compatible with printf! E153
00042     string tag = doc.RootElement()->Value();
00043     usb.printf("RootElement = <%s>\r\n\r\n", tag.c_str());
00044 
00045     // Iterate Elements with for loop.
00046     usb.printf("Enumerating Item Child Tags:\r\n");
00047     for (TiXmlNode* child = doc.RootElement()->FirstChild(); child; child = child->NextSibling() ) {
00048         if (child->Type()==TiXmlNode::TINYXML_ELEMENT) {
00049             tag = child->Value();
00050             usb.printf("  Child = <%s>\r\n", tag.c_str());
00051             usb.printf("  Text  = '%s'\r\n\r\n", child->ToElement()->GetText());
00052         }
00053     }
00054 
00055     //Walk Elements by FirstChildElement/NextSiblingElement.
00056     usb.printf("Walking Item Child Tags:\r\n");
00057     TiXmlElement* child = doc.RootElement()->FirstChildElement("Item");
00058 
00059     do {
00060         tag = child->Value();
00061         usb.printf("  Child = <%s>\r\n", tag.c_str());
00062         usb.printf("  Text  = '%s'\r\n\r\n", child->ToElement()->GetText());
00063 
00064         child = child->NextSiblingElement("Item");
00065     } while (child);
00066 
00067     //Save to a file on the /local/ filesystem on Usb.
00068     usb.printf("Saving with filename:\r\n");
00069     doc.SaveFile("/local/out_1.xml");
00070 
00071     //Save to a file on the /local/ filesystem on Usb.
00072     usb.printf("Saving with FILE:\r\n");
00073     FILE *fp = fopen("/local/out_2.xml", "w");
00074     doc.SaveFile(fp);
00075     fclose(fp);
00076 
00077     if ( doc.Error() ) {
00078         usb.printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
00079 
00080         exit( 1 );
00081     }
00082 
00083     //Print the Parsed Xml.
00084     usb.printf("Printing XML:\r\n");
00085     TiXmlPrinter printer;
00086     printer.SetLineBreak("\r\n"); //Default is "\n".
00087     doc.Accept(&printer);
00088 
00089     usb.printf("%s\r\n", printer.CStr());
00090 
00091     while (1) {
00092         //Wait...
00093     }
00094 }