32 Date() : day(0), month(0), year(0) { }
33 Date(Int_t d, Int_t m, Int_t y) : day(d), month(m), year(y) { }
34 Int_t GetDay()
const {
return day; }
35 Int_t GetMonth()
const {
return month; }
36 Int_t GetYear()
const {
return year; }
37 void SetDay(Int_t d) { day=d; }
38 void SetMonth(Int_t m) { month=m;}
39 void SetYear(Int_t y) { year=y;}
49 Address(TString s, TString p, TString c) :
50 street(s), postalCode(p), country(c) { }
51 TString GetStreet()
const {
return street; }
52 TString GetPostalCode()
const {
return postalCode; }
53 TString GetCountry()
const {
return country; }
54 void SetStreet(
const TString &s) { street = s; }
55 void SetPostalCode(
const TString &p) { postalCode = p; }
56 void SetCountry(
const TString &c) { country = c; }
63 class Person :
public TObject {
66 Person(Int_t i, TString f, TString l, Char_t g, Date * d, Address * a) :
67 id(i), firstName(f), lastName(l), gender(g), dateOfBirth(d), address(a){ }
74 TString GetFirstName()
const {
return firstName; }
75 TString GetLastName()
const {
return lastName; }
76 Char_t GetGender()
const {
return gender; }
77 Date *GetDate()
const {
return dateOfBirth; }
78 Address *GetAddress()
const {
return address; }
79 Int_t GetID()
const {
return id; }
81 friend ostream & operator << (ostream& out,
const Person& p) {
82 out <<
"ID: " << p.id << endl;
83 out <<
"First name: " << p.firstName << endl;
84 out <<
"Last name: " << p.lastName << endl;
85 out <<
"Sex: " << p.gender << endl;
86 out <<
"Date of birth: " << p.dateOfBirth->GetDay() <<
"/"
87 << p.dateOfBirth->GetMonth() <<
"/"
88 << p.dateOfBirth->GetYear() << endl;
89 out <<
"Address: " << p.address->GetStreet() << endl;
90 out <<
"\t" << p.address->GetPostalCode() << endl;
91 out <<
"\t" << p.address->GetCountry() << endl;
108 listOfPerson =
new TList();
111 Int_t ParseFile(TString filename) {
112 TDOMParser *domParser =
new TDOMParser();
113 Int_t parsecode = domParser->ParseFile(filename);
116 cerr << domParser->GetParseCodeMessage(parsecode) << endl;
120 TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
122 ParsePersonList(node);
127 void ParsePersonList(TXMLNode *node) {
128 for (; node; node = node->GetNextNode()) {
129 if (node->GetNodeType() == TXMLNode::kXMLElementNode) {
130 if (strcmp(node->GetNodeName(),
"Person") == 0) {
132 if (node->HasAttributes()) {
133 TList *attrList = node->GetAttributes();
135 TIter next(attrList);
136 while ((attr=(TXMLAttr*)next())) {
137 if (strcmp(attr->GetName(),
"ID") == 0) {
138 id = atoi(attr->GetValue());
143 listOfPerson->Add(ParsePerson(node->GetChildren(), id));
146 ParsePersonList(node->GetChildren());
150 Date *ParseDate(TXMLNode *node) {
152 for ( ; node; node = node->GetNextNode()) {
153 if (node->GetNodeType() == TXMLNode::kXMLElementNode) {
154 if (strcmp(node->GetNodeName(),
"Day") == 0) {
155 d = atoi(node->GetText());
157 if (strcmp(node->GetNodeName(),
"Month") == 0) {
158 m = atoi(node->GetText());
160 if (strcmp(node->GetNodeName(),
"Year") == 0) {
161 y = atoi(node->GetText());
165 return new Date(d, m, y);
168 Address *ParseAddress(TXMLNode *node) {
170 for( ; node!=NULL; node = node->GetNextNode()){
171 if (node->GetNodeType() == TXMLNode::kXMLElementNode) {
172 if (strcmp(node->GetNodeName(),
"Street") == 0) {
175 if (strcmp(node->GetNodeName(),
"PostalCode") == 0) {
178 if (strcmp(node->GetNodeName(),
"Country") == 0) {
183 return new Address(s, p, c);
186 Person *ParsePerson(TXMLNode *node, Int_t
id) {
187 TString firstName, lastName;
192 for ( ; node; node = node->GetNextNode()) {
193 if (node->GetNodeType() == TXMLNode::kXMLElementNode) {
194 if (strcmp(node->GetNodeName(),
"FirstName") == 0)
195 firstName = node->GetText();
196 if (strcmp(node->GetNodeName(),
"LastName") == 0)
197 lastName = node->GetText();
198 if (strcmp(node->GetNodeName(),
"Gender") == 0)
199 gender = node->GetText()[0];
200 if (strcmp(node->GetNodeName(),
"DateOfBirth") == 0)
201 date = ParseDate(node->GetChildren());
202 if (strcmp(node->GetNodeName(),
"Address") == 0)
203 address = ParseAddress(node->GetChildren());
207 return new Person(
id, firstName, lastName, gender, date, address);
210 friend ostream& operator << (ostream& out,
const PersonList & pl) {
211 TIter next(pl.listOfPerson);
213 while ((p =(Person*)next())){
220 TIter next(listOfPerson);
222 while ((p =(Person*)next())) {
228 Int_t numberOfPersons;
233 void DOMParsePerson()
235 PersonList personlist;
236 gROOT->ProcessLine(
".O 0");
237 TString dir = gROOT->GetTutorialDir();
238 if (personlist.ParseFile(dir+
"/xml/person.xml") == 0)
239 cout << personlist << endl;