testTinyXML.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string>
00003 #include <list>
00004 #include <sstream>
00005 #include <stdexcept>
00006 
00007 #include "TinyXML/tinyxml.h"
00008 
00009 using namespace std;
00010 
00011 
00012 int main(int argc, char* argv[]) {
00013   string xmlpath = "test.xml";
00014   if (argc > 1) xmlpath = argv[1];
00015 
00016   //popen("ls -l");
00017 
00018   TiXmlDocument doc(xmlpath);
00019   doc.LoadFile();
00020   if ( doc.Error() ) {
00021     cerr << "Error in " << doc.Value() << ": " << doc.ErrorDesc() << endl;
00022     return EXIT_FAILURE;
00023   }
00024 
00025 
00026   cout << "Getting bin edges from document..." << endl;
00027   try {
00028 
00029     // Walk down tree to get to the <paper> element
00030     const TiXmlNode* aidaN = doc.FirstChild("aida");
00031     if (!aidaN) throw runtime_error("Couldn't get <aida> root element");
00032     for (const TiXmlNode* dpsN = aidaN->FirstChild("dataPointSet"); dpsN; dpsN = dpsN->NextSibling()) {
00033       const TiXmlElement* dpsE = dpsN->ToElement();
00034       cout << "DataPointSet: " << dpsE->Attribute("name") << endl;
00035       list<double> edges;
00036       for (const TiXmlNode* dpN = dpsN->FirstChild("dataPoint"); dpN; dpN = dpN->NextSibling()) {
00037         const TiXmlNode* xMeasN = dpN->FirstChild("measurement");
00038         if (xMeasN) {
00039           const TiXmlElement* xMeasE = xMeasN->ToElement();
00040           const string centreStr = xMeasE->Attribute("value");
00041           const string errplusStr = xMeasE->Attribute("errorPlus"); 
00042           const string errminusStr = xMeasE->Attribute("errorMinus"); 
00043           istringstream ssC(centreStr);
00044           istringstream ssP(errplusStr);
00045           istringstream ssM(errminusStr);
00046           double centre, errplus, errminus;
00047           ssC >> centre; ssP >> errplus; ssM >> errminus;
00048           cout << "  " << centre << " + " << errplus << " - " << errminus << endl;
00049           const double lowedge = centre - errminus;
00050           const double highedge = centre + errplus;
00051           edges.push_back(lowedge);
00052           edges.push_back(highedge);
00053         } else {
00054           cerr << "Couldn't get <measurement> tag" << endl;
00055         }
00056       }
00057 
00058       // Print out the edges
00059       cout << "Raw: [";
00060       for (list<double>::const_iterator e = edges.begin(); e != edges.end(); ++e) {
00061         cout << *e << " ";
00062       }
00063       cout << "]" << endl;
00064 
00065       // Remove duplicates(within fractional 10^-3)
00066       for (list<double>::iterator e = edges.begin(); e != edges.end(); ++e) {
00067         list<double>::iterator e2 = e;
00068         while (e2 != edges.end()) {
00069           if (e != e2 && *e == *e2) {
00070             edges.erase(e2++);
00071           } else {
00072             ++e2;
00073           }
00074         }
00075       }
00076 
00077       // Print out the edges
00078       cout << "Stripped: [";
00079       for (list<double>::const_iterator e = edges.begin(); e != edges.end(); ++e) {
00080         cout << *e << " ";
00081       }
00082       cout << "]" << endl;
00083       cout << endl;
00084     }
00085 
00086   } 
00087   catch (exception& e) {
00088     cerr << e.what() << endl;
00089     return EXIT_FAILURE;
00090   }
00091   return EXIT_SUCCESS;
00092 }