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() { }
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; }
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;
}
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) {
id = atoi(
attr->GetValue());
break;
}
}
}
listOfPerson->Add(ParsePerson(node->
GetChildren(),
id));
}
}
}
}
}
}
}
}
}
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);
while ((
p =(Person*)next())){
}
}
void PrintPerson() {
TIter next(listOfPerson);
while ((
p =(Person*)next())) {
}
}
private:
};
void DOMParsePerson()
{
PersonList personlist;
gROOT->ProcessLine(
".O 0");
if (personlist.ParseFile(dir+"/xml/person.xml") == 0)
cout << personlist << endl;
}
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
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.
TXMLAttribute is the attribute of an Element.
TXMLNode * GetRootNode() const
Returns the root element node.
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
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)