ROOT implementation of a XML DOM Parser
This is an example of how Dom Parser works. It will parse the xml file (person.xml) to the Person object. A DTD validation will be run on this example.
To run this program
Requires: person.xml and person.dtd
0.00943994522095
2.72852110863
Processing /mnt/build/workspace/root-makedoc-v612/rootspi/rdoc/src/v6-12-00-patches/tutorials/xml/DOMParsePerson.C...
ID: 1
First name: Alicia
Last name: Smith
Sex: F
Date of birth: 13/10/1978
Address: Grand Avenue, 143
Toronto 2283
Canada
ID: 2
First name: Maria
Last name: White
Sex: F
Date of birth: 29/5/1980
Address: Green Land Park, 143
Vancouver BC V6C 2C2
Canada
class Date {
public:
Date() : day(0), month(0), year(0) { }
Int_t GetDay()
const {
return day; }
Int_t GetMonth()
const {
return month; }
Int_t GetYear()
const {
return year; }
void SetDay(
Int_t d) { day=d; }
void SetMonth(
Int_t m) { month=
m;}
void SetYear(
Int_t y) { year=
y;}
private:
};
class Address {
public:
Address() { }
street(s), postalCode(p), country(c) { }
TString GetStreet()
const {
return street; }
TString GetPostalCode()
const {
return postalCode; }
TString GetCountry()
const {
return country; }
void SetStreet(
const TString &s) { street =
s; }
void SetPostalCode(
const TString &p) { postalCode = p; }
void SetCountry(
const TString &c) { country = c; }
private:
};
public:
Person() { }
id(i), firstName(f), lastName(l), gender(g), dateOfBirth(d), address(a){ }
~Person() {
delete dateOfBirth;
delete address;
}
TString GetFirstName()
const {
return firstName; }
TString GetLastName()
const {
return lastName; }
Char_t GetGender()
const {
return gender; }
Date *GetDate() const { return dateOfBirth; }
Address *GetAddress() const { return address; }
friend ostream &
operator << (ostream& out,
const Person& p) {
out << "ID: " << p.id << endl;
out << "First name: " << p.firstName << endl;
out << "Last name: " << p.lastName << endl;
out << "Sex: " << p.gender << endl;
out << "Date of birth: " << p.dateOfBirth->GetDay() << "/"
<< p.dateOfBirth->GetMonth() << "/"
<< p.dateOfBirth->GetYear() << endl;
out << "Address: " << p.address->GetStreet() << endl;
out << "\t" << p.address->GetPostalCode() << endl;
out << "\t" << p.address->GetCountry() << endl;
out << endl;
return out;
}
private:
Date *dateOfBirth;
Address *address;
};
class PersonList {
public:
PersonList() {
listOfPerson =
new TList();
}
if (parsecode < 0) {
return -1;
}
ParsePersonList(node);
return 0;
}
if (strcmp(attr->
GetName(),
"ID") == 0) {
break;
}
}
}
}
}
}
}
}
}
}
}
}
return new Date(d, m, y);
}
}
}
}
}
}
return new Address(s, p, c);
}
char gender = ' ';
Date *date;
Address *address;
}
}
return new Person(id, firstName, lastName, gender, date, address);
}
friend ostream&
operator << (ostream& out,
const PersonList & pl) {
TIter next(pl.listOfPerson);
Person *p;
while ((p =(Person*)next())){
out << *p << endl;
}
return out;
}
void PrintPerson() {
TIter next(listOfPerson);
Person *p;
while ((p =(Person*)next())) {
cout << *p << endl;
}
}
private:
};
void DOMParsePerson()
{
PersonList personlist;
gROOT->ProcessLine(
".O 0");
if (personlist.ParseFile(dir+"/xml/person.xml") == 0)
cout << personlist << endl;
}
- Author
- Sergey Linev
Definition in file DOMParsePerson.C.