Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DOMParsePerson.C File Reference

Detailed Description

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

.x DOMParsePerson.C+

Requires: person.xml and person.dtd

#include <Riostream.h>
#include <TDOMParser.h>
#include <TXMLAttr.h>
#include <TXMLNode.h>
#include <TList.h>
class Date {
public:
Date() : day(0), month(0), year(0) { }
Date(Int_t d, Int_t m, Int_t y) : day(d), month(m), year(y) { }
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:
Int_t day;
Int_t month;
Int_t year;
};
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;
};
class Person : public TObject {
public:
Person() { }
Person(Int_t i, TString f, TString l, Char_t g, Date * d, Address * a) :
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; }
Int_t GetID() const { return id; }
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;
Char_t gender;
Date *dateOfBirth;
Address *address;
};
class PersonList {
public:
PersonList() {
listOfPerson = new TList();
}
Int_t ParseFile(TString filename) {
TDOMParser *domParser = new TDOMParser();
Int_t parsecode = domParser->ParseFile(filename);
if (parsecode < 0) {
cerr << domParser->GetParseCodeMessage(parsecode) << endl;
return -1;
}
TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
ParsePersonList(node);
return 0;
}
void ParsePersonList(TXMLNode *node) {
for (; node; node = node->GetNextNode()) {
if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
if (strcmp(node->GetNodeName(), "Person") == 0) {
Int_t id=0;
if (node->HasAttributes()) {
TList *attrList = node->GetAttributes();
TXMLAttr *attr = 0;
TIter next(attrList);
while ((attr=(TXMLAttr*)next())) {
if (strcmp(attr->GetName(), "ID") == 0) {
id = atoi(attr->GetValue());
break;
}
}
}
listOfPerson->Add(ParsePerson(node->GetChildren(), id));
}
}
ParsePersonList(node->GetChildren());
}
}
Date *ParseDate(TXMLNode *node) {
Int_t d=0, m=0, y=0;
for ( ; node; node = node->GetNextNode()) {
if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
if (strcmp(node->GetNodeName(), "Day") == 0) {
d = atoi(node->GetText());
}
if (strcmp(node->GetNodeName(), "Month") == 0) {
m = atoi(node->GetText());
}
if (strcmp(node->GetNodeName(), "Year") == 0) {
y = atoi(node->GetText());
}
}
}
return new Date(d, m, y);
}
Address *ParseAddress(TXMLNode *node) {
TString s, p, c;
for( ; node!=NULL; node = node->GetNextNode()){
if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
if (strcmp(node->GetNodeName(), "Street") == 0) {
s = node->GetText();
}
if (strcmp(node->GetNodeName(), "PostalCode") == 0) {
p = node->GetText();
}
if (strcmp(node->GetNodeName(), "Country") == 0) {
c = node->GetText();
}
}
}
return new Address(s, p, c);
}
Person *ParsePerson(TXMLNode *node, Int_t id) {
TString firstName, lastName;
char gender = ' ';
Date *date;
Address *address;
for ( ; node; node = node->GetNextNode()) {
if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
if (strcmp(node->GetNodeName(), "FirstName") == 0)
firstName = node->GetText();
if (strcmp(node->GetNodeName(), "LastName") == 0)
lastName = node->GetText();
if (strcmp(node->GetNodeName(), "Gender") == 0)
gender = node->GetText()[0];
if (strcmp(node->GetNodeName(), "DateOfBirth") == 0)
date = ParseDate(node->GetChildren());
if (strcmp(node->GetNodeName(), "Address") == 0)
address = ParseAddress(node->GetChildren());
}
}
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:
Int_t numberOfPersons;
TList *listOfPerson;
};
void DOMParsePerson()
{
PersonList personlist;
gROOT->ProcessLine(".O 0");
TString dir = gROOT->GetTutorialDir();
if (personlist.ParseFile(dir+"/xml/person.xml") == 0)
cout << personlist << endl;
}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
int Int_t
Definition RtypesCore.h:45
char Char_t
Definition RtypesCore.h:37
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
#define gROOT
Definition TROOT.h:405
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.
A doubly linked list.
Definition TList.h:38
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:139
TXMLAttribute is the attribute of an Element.
Definition TXMLAttr.h:18
TXMLNode * GetRootNode() const
Returns the root element node.
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
Definition TXMLNode.h:20
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition TXMLNode.cxx:108
const char * GetText() const
Returns the content of a Text node if node is a TextNode, 0 otherwise.
Definition TXMLNode.cxx:154
@ kXMLElementNode
Definition TXMLNode.h:37
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition TXMLNode.cxx:130
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition TXMLNode.cxx:74
const char * GetNodeName() const
Returns the node's name.
Definition TXMLNode.cxx:66
Bool_t HasAttributes() const
Returns true if Element node has attribute.
Definition TXMLNode.cxx:198
EXMLElementType GetNodeType() const
Returns the node's type.
Definition TXMLNode.cxx:58
const char * GetParseCodeMessage(Int_t parseCode) const
Returns the parse code message.
Double_t y[n]
Definition legend1.C:17
ULong64_t GetAddress(std::vector< std::string > &p)
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
Author
Sergey Linev

Definition in file DOMParsePerson.C.