Logo ROOT   6.18/05
Reference Guide
DOMParsePerson.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_xml
3/// \notebook -nodraw
4/// ROOT implementation of a XML DOM Parser
5///
6/// This is an example of how Dom Parser works. It will parse the xml file
7/// (person.xml) to the Person object.
8/// A DTD validation will be run on this example.
9///
10/// To run this program
11/// ~~~{.cpp}
12/// .x DOMParsePerson.C+
13/// ~~~
14///
15/// Requires: person.xml and person.dtd
16///
17/// \macro_output
18/// \macro_code
19///
20/// \author Sergey Linev
21
22
23#include <Riostream.h>
24#include <TDOMParser.h>
25#include <TXMLAttr.h>
26#include <TXMLNode.h>
27#include <TList.h>
28
29
30class Date {
31public:
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;}
40private:
41 Int_t day;
42 Int_t month;
43 Int_t year;
44};
45
46class Address {
47public:
48 Address() { }
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; }
57private:
58 TString street;
59 TString postalCode;
60 TString country;
61};
62
63class Person : public TObject {
64public:
65 Person() { }
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){ }
68
69 ~Person() {
70 delete dateOfBirth;
71 delete address;
72 }
73
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; }
80
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;
92 out << endl;
93 return out;
94 }
95
96private:
97 Int_t id;
98 TString firstName;
99 TString lastName;
100 Char_t gender;
101 Date *dateOfBirth;
102 Address *address;
103};
104
105class PersonList {
106public:
107 PersonList() {
108 listOfPerson = new TList();
109 }
110
111 Int_t ParseFile(TString filename) {
112 TDOMParser *domParser = new TDOMParser();
113 Int_t parsecode = domParser->ParseFile(filename);
114
115 if (parsecode < 0) {
116 cerr << domParser->GetParseCodeMessage(parsecode) << endl;
117 return -1;
118 }
119
120 TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
121
122 ParsePersonList(node);
123
124 return 0;
125 }
126
127 void ParsePersonList(TXMLNode *node) {
128 for (; node; node = node->GetNextNode()) {
129 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
130 if (strcmp(node->GetNodeName(), "Person") == 0) {
131 Int_t id=0;
132 if (node->HasAttributes()) {
133 TList *attrList = node->GetAttributes();
134 TXMLAttr *attr = 0;
135 TIter next(attrList);
136 while ((attr=(TXMLAttr*)next())) {
137 if (strcmp(attr->GetName(), "ID") == 0) {
138 id = atoi(attr->GetValue());
139 break;
140 }
141 }
142 }
143 listOfPerson->Add(ParsePerson(node->GetChildren(), id));
144 }
145 }
146 ParsePersonList(node->GetChildren());
147 }
148 }
149
150 Date *ParseDate(TXMLNode *node) {
151 Int_t d=0, m=0, y=0;
152 for ( ; node; node = node->GetNextNode()) {
153 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
154 if (strcmp(node->GetNodeName(), "Day") == 0) {
155 d = atoi(node->GetText());
156 }
157 if (strcmp(node->GetNodeName(), "Month") == 0) {
158 m = atoi(node->GetText());
159 }
160 if (strcmp(node->GetNodeName(), "Year") == 0) {
161 y = atoi(node->GetText());
162 }
163 }
164 }
165 return new Date(d, m, y);
166 }
167
168 Address *ParseAddress(TXMLNode *node) {
169 TString s, p, c;
170 for( ; node!=NULL; node = node->GetNextNode()){
171 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
172 if (strcmp(node->GetNodeName(), "Street") == 0) {
173 s = node->GetText();
174 }
175 if (strcmp(node->GetNodeName(), "PostalCode") == 0) {
176 p = node->GetText();
177 }
178 if (strcmp(node->GetNodeName(), "Country") == 0) {
179 c = node->GetText();
180 }
181 }
182 }
183 return new Address(s, p, c);
184 }
185
186 Person *ParsePerson(TXMLNode *node, Int_t id) {
187 TString firstName, lastName;
188 char gender = ' ';
189 Date *date;
190 Address *address;
191
192 for ( ; node; node = node->GetNextNode()) {
193 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
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());
204 }
205 }
206
207 return new Person(id, firstName, lastName, gender, date, address);
208 }
209
210 friend ostream& operator << (ostream& out, const PersonList & pl) {
211 TIter next(pl.listOfPerson);
212 Person *p;
213 while ((p =(Person*)next())){
214 out << *p << endl;
215 }
216 return out;
217 }
218
219 void PrintPerson() {
220 TIter next(listOfPerson);
221 Person *p;
222 while ((p =(Person*)next())) {
223 cout << *p << endl;
224 }
225 }
226
227private:
228 Int_t numberOfPersons;
229 TList *listOfPerson;
230};
231
232
233void DOMParsePerson()
234{
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;
240}
#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
int Int_t
Definition: RtypesCore.h:41
char Char_t
Definition: RtypesCore.h:29
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition: TBuffer.h:398
XFontStruct * id
Definition: TGX11.cxx:108
#define gROOT
Definition: TROOT.h:414
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Definition: TDOMParser.cxx:144
virtual Int_t ParseFile(const char *filename)
Parse the XML file where filename is the XML file name.
Definition: TDOMParser.cxx:70
A doubly linked list.
Definition: TList.h:44
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:131
TXMLAttribute is the attribute of an Element.
Definition: TXMLAttr.h:18
const char * GetValue() const
Definition: TXMLAttr.h:33
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:31
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:22
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:39
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.
Definition: TXMLParser.cxx:123
Double_t y[n]
Definition: legend1.C:17
ULong64_t GetAddress(std::vector< std::string > &p)
static constexpr double s
auto * m
Definition: textangle.C:8
auto * l
Definition: textangle.C:4
auto * a
Definition: textangle.C:12