ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DOMParsePerson.C
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // ROOT implementation of a XML DOM Parser
4 //
5 // This is an example of how Dom Parser works. It will parse the xml file
6 // (person.xml) to the Person object.
7 // A DTD validation will be run on this example.
8 //
9 // To run this program
10 // .x DOMParsePerson.C+
11 //
12 // Requires: person.xml and person.dtd
13 //
14 //////////////////////////////////////////////////////////////////////////////
15 
16 #include <Riostream.h>
17 #include <TDOMParser.h>
18 #include <TXMLAttr.h>
19 #include <TXMLNode.h>
20 #include <TList.h>
21 
22 
23 class Date {
24 public:
25  Date() : day(0), month(0), year(0) { }
26  Date(Int_t d, Int_t m, Int_t y) : day(d), month(m), year(y) { }
27  Int_t GetDay() const { return day; }
28  Int_t GetMonth() const { return month; }
29  Int_t GetYear() const { return year; }
30  void SetDay(Int_t d) { day=d; }
31  void SetMonth(Int_t m) { month=m;}
32  void SetYear(Int_t y) { year=y;}
33 private:
34  Int_t day;
35  Int_t month;
36  Int_t year;
37 };
38 
39 class Address {
40 public:
41  Address() { }
42  Address(TString s, TString p, TString c) :
43  street(s), postalCode(p), country(c) { }
44  TString GetStreet() const { return street; }
45  TString GetPostalCode() const { return postalCode; }
46  TString GetCountry() const { return country; }
47  void SetStreet(const TString &s) { street = s; }
48  void SetPostalCode(const TString &p) { postalCode = p; }
49  void SetCountry(const TString &c) { country = c; }
50 private:
51  TString street;
52  TString postalCode;
53  TString country;
54 };
55 
56 class Person : public TObject {
57 public:
58  Person() { }
59  Person(Int_t i, TString f, TString l, Char_t g, Date * d, Address * a) :
60  id(i), firstName(f), lastName(l), gender(g), dateOfBirth(d), address(a){ }
61 
62  ~Person() {
63  delete dateOfBirth;
64  delete address;
65  }
66 
67  TString GetFirstName() const { return firstName; }
68  TString GetLastName() const { return lastName; }
69  Char_t GetGender() const { return gender; }
70  Date *GetDate() const { return dateOfBirth; }
71  Address *GetAddress() const { return address; }
72  Int_t GetID() const { return id; }
73 
74  friend ostream & operator << (ostream& out, const Person& p) {
75  out << "ID: " << p.id << endl;
76  out << "First name: " << p.firstName << endl;
77  out << "Last name: " << p.lastName << endl;
78  out << "Sex: " << p.gender << endl;
79  out << "Date of birth: " << p.dateOfBirth->GetDay() << "/"
80  << p.dateOfBirth->GetMonth() << "/"
81  << p.dateOfBirth->GetYear() << endl;
82  out << "Address: " << p.address->GetStreet() << endl;
83  out << "\t" << p.address->GetPostalCode() << endl;
84  out << "\t" << p.address->GetCountry() << endl;
85  out << endl;
86  return out;
87  }
88 
89 private:
90  Int_t id;
91  TString firstName;
92  TString lastName;
93  Char_t gender;
94  Date *dateOfBirth;
95  Address *address;
96 };
97 
98 class PersonList {
99 public:
100  PersonList() {
101  listOfPerson = new TList();
102  }
103 
104  Int_t ParseFile(TString filename) {
105  TDOMParser *domParser = new TDOMParser();
106  Int_t parsecode = domParser->ParseFile(filename);
107 
108  if (parsecode < 0) {
109  cerr << domParser->GetParseCodeMessage(parsecode) << endl;
110  return -1;
111  }
112 
113  TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
114 
115  ParsePersonList(node);
116 
117  return 0;
118  }
119 
120  void ParsePersonList(TXMLNode *node) {
121  for (; node; node = node->GetNextNode()) {
122  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
123  if (strcmp(node->GetNodeName(), "Person") == 0) {
124  Int_t id=0;
125  if (node->HasAttributes()) {
126  TList *attrList = node->GetAttributes();
127  TXMLAttr *attr = 0;
128  TIter next(attrList);
129  while ((attr=(TXMLAttr*)next())) {
130  if (strcmp(attr->GetName(), "ID") == 0) {
131  id = atoi(attr->GetValue());
132  break;
133  }
134  }
135  }
136  listOfPerson->Add(ParsePerson(node->GetChildren(), id));
137  }
138  }
139  ParsePersonList(node->GetChildren());
140  }
141  }
142 
143  Date *ParseDate(TXMLNode *node) {
144  Int_t d=0, m=0, y=0;
145  for ( ; node; node = node->GetNextNode()) {
146  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
147  if (strcmp(node->GetNodeName(), "Day") == 0) {
148  d = atoi(node->GetText());
149  }
150  if (strcmp(node->GetNodeName(), "Month") == 0) {
151  m = atoi(node->GetText());
152  }
153  if (strcmp(node->GetNodeName(), "Year") == 0) {
154  y = atoi(node->GetText());
155  }
156  }
157  }
158  return new Date(d, m, y);
159  }
160 
161  Address *ParseAddress(TXMLNode *node) {
162  TString s, p, c;
163  for( ; node!=NULL; node = node->GetNextNode()){
164  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
165  if (strcmp(node->GetNodeName(), "Street") == 0) {
166  s = node->GetText();
167  }
168  if (strcmp(node->GetNodeName(), "PostalCode") == 0) {
169  p = node->GetText();
170  }
171  if (strcmp(node->GetNodeName(), "Country") == 0) {
172  c = node->GetText();
173  }
174  }
175  }
176  return new Address(s, p, c);
177  }
178 
179  Person *ParsePerson(TXMLNode *node, Int_t id) {
180  TString firstName, lastName;
181  char gender = ' ';
182  Date *date;
183  Address *address;
184 
185  for ( ; node; node = node->GetNextNode()) {
186  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
187  if (strcmp(node->GetNodeName(), "FirstName") == 0)
188  firstName = node->GetText();
189  if (strcmp(node->GetNodeName(), "LastName") == 0)
190  lastName = node->GetText();
191  if (strcmp(node->GetNodeName(), "Gender") == 0)
192  gender = node->GetText()[0];
193  if (strcmp(node->GetNodeName(), "DateOfBirth") == 0)
194  date = ParseDate(node->GetChildren());
195  if (strcmp(node->GetNodeName(), "Address") == 0)
196  address = ParseAddress(node->GetChildren());
197  }
198  }
199 
200  return new Person(id, firstName, lastName, gender, date, address);
201  }
202 
203  friend ostream& operator << (ostream& out, const PersonList & pl) {
204  TIter next(pl.listOfPerson);
205  Person *p;
206  while ((p =(Person*)next())){
207  out << *p << endl;
208  }
209  return out;
210  }
211 
212  void PrintPerson() {
213  TIter next(listOfPerson);
214  Person *p;
215  while ((p =(Person*)next())) {
216  cout << *p << endl;
217  }
218  }
219 
220 private:
221  Int_t numberOfPersons;
222  TList *listOfPerson;
223 };
224 
225 
227 {
228  PersonList personlist;
229  gROOT->ProcessLine(".O 0");
230  TString dir = gSystem->DirName(__FILE__);
231  if (personlist.ParseFile(dir+"/person.xml") == 0)
232  cout << personlist << endl;
233 }
const char * GetNodeName() const
Returns the node's name.
Definition: TXMLNode.cxx:66
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
return c
#define gROOT
Definition: TROOT.h:344
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:980
TArc * a
Definition: textangle.C:12
TXMLNode * GetRootNode() const
Returns the root element node.
TFile * f
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Definition: TDOMParser.cxx:144
Bool_t HasAttributes() const
Returns true if Element node has attribute.
Definition: TXMLNode.cxx:198
int d
Definition: tornado.py:11
XFontStruct * id
Definition: TGX11.cxx:108
char * out
Definition: TBase64.cxx:29
A doubly linked list.
Definition: TList.h:47
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition: TXMLNode.cxx:130
virtual Int_t ParseFile(const char *filename)
Parse the XML file where filename is the XML file name.
Definition: TDOMParser.cxx:70
void DOMParsePerson()
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
const char * GetParseCodeMessage(Int_t parseCode) const
Returns the parse code message.
Definition: TXMLParser.cxx:123
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition: TXMLNode.cxx:108
TMarker * m
Definition: textangle.C:8
TLine * l
Definition: textangle.C:4
TXMLAttribute is the attribute of an Element.
Definition: TXMLAttr.h:20
const char * GetValue() const
Definition: TXMLAttr.h:35
EXMLElementType GetNodeType() const
Returns the node's type.
Definition: TXMLNode.cxx:58
void dir(char *path=0)
Definition: rootalias.C:30
Double_t y[n]
Definition: legend1.C:17
Mother of all ROOT objects.
Definition: TObject.h:58
const char * GetText() const
Returns the content of a Text node if node is a TextNode, 0 otherwise.
Definition: TXMLNode.cxx:154
char Char_t
Definition: RtypesCore.h:29
#define NULL
Definition: Rtypes.h:82
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:33
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
Definition: TXMLNode.h:26
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition: TXMLNode.cxx:74
ROOT::R::TRInterface & operator<<(ROOT::R::TRInterface &r, TString code)
Definition: TRInterface.h:327