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.
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; }
private:
};
class Address {
public:
Address() { }
Address(TString s, TString p, TString
c) :
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:
TString street;
TString postalCode;
TString country;
};
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; }
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:
TString firstName;
TString lastName;
Date *dateOfBirth;
Address *address;
};
class PersonList {
public:
PersonList() {
listOfPerson = new TList();
}
Int_t ParseFile(TString filename) {
TDOMParser *domParser = new TDOMParser();
if (parsecode < 0) {
return -1;
}
ParsePersonList(node);
return 0;
}
void ParsePersonList(TXMLNode *node) {
TXMLAttr *attr = 0;
TIter next(attrList);
while ((attr=(TXMLAttr*)next())) {
if (strcmp(attr->
GetName(),
"ID") == 0) {
break;
}
}
}
listOfPerson->Add(ParsePerson(node->
GetChildren(),
id));
}
}
}
}
Date *ParseDate(TXMLNode *node) {
}
}
}
}
}
return new Date(
d,
m,
y);
}
Address *ParseAddress(TXMLNode *node) {
}
}
}
}
}
return new Address(s, p,
c);
}
Person *ParsePerson(TXMLNode *node,
Int_t id) {
TString firstName, lastName;
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:
TList *listOfPerson;
};
void DOMParsePerson()
{
PersonList personlist;
gROOT->ProcessLine(
".O 0");
if (personlist.ParseFile(dir+"/xml/person.xml") == 0)
cout << personlist << endl;
}
int Int_t
Signed integer 4 bytes (int).
char Char_t
Character 1 byte (char).
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Int_t ParseFile(const char *filename) override
Parse the XML file where filename is the XML file name.
Mother of all ROOT objects.
const char * GetValue() const
const char * GetName() const override
Returns name of object.
TXMLNode * GetRootNode() const
Returns the root element node.
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
const char * GetText() const
Returns the content of a Text node if node is a TextNode, 0 otherwise.
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
const char * GetNodeName() const
Returns the node's name.
Bool_t HasAttributes() const
Returns true if Element node has attribute.
EXMLElementType GetNodeType() const
Returns the node's type.
const char * GetParseCodeMessage(Int_t parseCode) const
Returns the parse code message.
ULong64_t GetAddress(std::vector< std::string > &p)