DOMParsePerson.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////
00002 //
00003 // ROOT implementation of a XML DOM Parser                                    
00004 //                                                                            
00005 // This is an example of how Dom Parser works. It will parse the xml file     
00006 // (person.xml) to the Person object.                                         
00007 // A DTD validation will be run on this example.                              
00008 //                                                                            
00009 // To run this program                                                        
00010 // .x DOMParsePerson.C+                                                        
00011 //                                                                            
00012 // Requires: person.xml and person.dtd                                        
00013 //                                                                            
00014 //////////////////////////////////////////////////////////////////////////////
00015 
00016 #include <Riostream.h>
00017 #include <TDOMParser.h>
00018 #include <TXMLAttr.h>
00019 #include <TXMLNode.h>
00020 #include <TList.h>
00021 
00022 
00023 class Date {
00024 public:
00025    Date() : day(0), month(0), year(0) { }
00026    Date(Int_t d, Int_t m, Int_t y) : day(d), month(m), year(y) { }
00027    Int_t GetDay() const { return day; }
00028    Int_t GetMonth() const { return month; }
00029    Int_t GetYear() const { return year; }
00030    void SetDay(Int_t d) { day=d; }
00031    void SetMonth(Int_t m) { month=m;}
00032    void SetYear(Int_t y) { year=y;}
00033 private:
00034    Int_t day;
00035    Int_t month;
00036    Int_t year;
00037 };
00038 
00039 class Address {
00040 public:
00041    Address() { }
00042    Address(TString s, TString p, TString c) :
00043       street(s), postalCode(p), country(c) { }
00044    TString GetStreet() const { return street; }
00045    TString GetPostalCode() const { return postalCode; }
00046    TString GetCountry() const { return country; }
00047    void SetStreet(const TString &s) { street = s; }
00048    void SetPostalCode(const TString &p) { postalCode = p; }
00049    void SetCountry(const TString &c) { country = c; }
00050 private:
00051    TString street;
00052    TString postalCode;
00053    TString country;
00054 };
00055 
00056 class Person : public TObject {
00057 public:
00058    Person() { }
00059    Person(Int_t i, TString f, TString l, Char_t g, Date * d, Address * a) :
00060       id(i), firstName(f), lastName(l), gender(g), dateOfBirth(d), address(a){ }
00061 
00062    ~Person() {
00063       delete dateOfBirth;
00064       delete address;
00065    }
00066 
00067    TString GetFirstName() const { return firstName; }
00068    TString GetLastName() const { return lastName; }
00069    Char_t GetGender() const { return gender; }
00070    Date *GetDate() const { return dateOfBirth; }
00071    Address *GetAddress() const { return address; }
00072    Int_t GetID() const { return id; }
00073 
00074    friend ostream & operator << (ostream& out, const Person& p) {
00075       out << "ID: " << p.id << endl;
00076       out << "First name: " << p.firstName << endl;
00077       out << "Last name: " << p.lastName << endl;
00078       out << "Sex: " << p.gender << endl;
00079       out << "Date of birth: " << p.dateOfBirth->GetDay() << "/"
00080           << p.dateOfBirth->GetMonth() << "/"
00081           << p.dateOfBirth->GetYear() << endl;
00082       out << "Address: " << p.address->GetStreet() << endl;
00083       out << "\t" << p.address->GetPostalCode() << endl;
00084       out << "\t" << p.address->GetCountry() << endl;
00085       out << endl;
00086       return out;
00087    }
00088 
00089 private:
00090    Int_t     id;
00091    TString   firstName;
00092    TString   lastName;
00093    Char_t    gender;
00094    Date     *dateOfBirth;
00095    Address  *address;
00096 };
00097 
00098 class PersonList {
00099 public:
00100    PersonList() {
00101       listOfPerson = new TList();
00102    }
00103 
00104    Int_t ParseFile(TString filename) {
00105       TDOMParser *domParser = new TDOMParser();
00106       Int_t parsecode = domParser->ParseFile(filename);
00107 
00108       if (parsecode < 0) {
00109          cerr << domParser->GetParseCodeMessage(parsecode) << endl;
00110          return -1;
00111       }
00112 
00113       TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
00114 
00115       ParsePersonList(node);
00116 
00117       return 0;
00118    }
00119 
00120    void ParsePersonList(TXMLNode *node) {
00121       for (; node; node = node->GetNextNode()) {
00122          if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
00123             if (strcmp(node->GetNodeName(), "Person") == 0) {
00124                Int_t id=0;
00125                if (node->HasAttributes()) {
00126                   TList *attrList = node->GetAttributes();
00127                   TXMLAttr *attr = 0;
00128                   TIter next(attrList);
00129                   while ((attr=(TXMLAttr*)next())) {
00130                      if (strcmp(attr->GetName(), "ID") == 0) {
00131                         id = atoi(attr->GetValue());
00132                         break;
00133                      }
00134                   }
00135                }
00136                listOfPerson->Add(ParsePerson(node->GetChildren(), id));
00137             }
00138          }
00139          ParsePersonList(node->GetChildren());
00140       }
00141    }
00142 
00143    Date *ParseDate(TXMLNode *node) {
00144       Int_t d=0, m=0, y=0;
00145       for ( ; node; node = node->GetNextNode()) {
00146          if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
00147             if (strcmp(node->GetNodeName(), "Day") == 0) {
00148                d = atoi(node->GetText());
00149             }
00150             if (strcmp(node->GetNodeName(), "Month") == 0) {
00151                m = atoi(node->GetText());
00152             }
00153             if (strcmp(node->GetNodeName(), "Year") == 0) {
00154                y = atoi(node->GetText());
00155             }
00156          }
00157       }
00158       return new Date(d, m, y);
00159    }
00160 
00161    Address *ParseAddress(TXMLNode *node) {
00162       TString s, p, c;
00163       for( ; node!=NULL; node = node->GetNextNode()){
00164          if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
00165             if (strcmp(node->GetNodeName(), "Street") == 0) {
00166                s = node->GetText();
00167             }
00168             if (strcmp(node->GetNodeName(), "PostalCode") == 0) {
00169                p = node->GetText();
00170             }
00171             if (strcmp(node->GetNodeName(), "Country") == 0) {
00172                c = node->GetText();
00173             }
00174          }
00175      }
00176      return new Address(s, p, c);
00177    }
00178 
00179    Person *ParsePerson(TXMLNode *node, Int_t id) {
00180       TString firstName, lastName;
00181       char gender = ' ';
00182       Date *date;
00183       Address *address;
00184 
00185       for ( ; node; node = node->GetNextNode()) {
00186          if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
00187             if (strcmp(node->GetNodeName(), "FirstName") == 0)
00188                firstName = node->GetText();
00189             if (strcmp(node->GetNodeName(), "LastName") == 0)
00190                lastName = node->GetText();
00191             if (strcmp(node->GetNodeName(), "Gender") == 0)
00192                gender = node->GetText()[0];
00193             if (strcmp(node->GetNodeName(), "DateOfBirth") == 0)
00194                date = ParseDate(node->GetChildren());
00195             if (strcmp(node->GetNodeName(), "Address") == 0)
00196                address = ParseAddress(node->GetChildren());
00197          }
00198      }
00199 
00200      return new Person(id, firstName, lastName, gender, date, address);
00201    }
00202 
00203    friend ostream& operator << (ostream& out, const PersonList & pl) {
00204       TIter next(pl.listOfPerson);
00205       Person *p;
00206       while ((p =(Person*)next())){
00207          out << *p << endl;
00208       }
00209       return out;
00210    }
00211 
00212    void PrintPerson() {
00213       TIter next(listOfPerson);
00214       Person *p;
00215       while ((p =(Person*)next())) {
00216          cout << *p << endl;
00217       }
00218    }
00219 
00220 private:
00221    Int_t  numberOfPersons;
00222    TList *listOfPerson;
00223 };
00224 
00225 
00226 void DOMParsePerson()
00227 {
00228    PersonList personlist;
00229 
00230    if (personlist.ParseFile("person.xml") == 0)
00231       cout << personlist << endl;
00232 }

Generated on Tue Jul 5 15:45:12 2011 for ROOT_528-00b_version by  doxygen 1.5.1