Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DOMParsePerson.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_xml
3///
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_code
18///
19/// \author Sergey Linev
20
21
22#include <Riostream.h>
23#include <TDOMParser.h>
24#include <TXMLAttr.h>
25#include <TXMLNode.h>
26#include <TList.h>
27
28
29class Date {
30public:
31 Date() : day(0), month(0), year(0) { }
32 Date(Int_t d, Int_t m, Int_t y) : day(d), month(m), year(y) { }
33 Int_t GetDay() const { return day; }
34 Int_t GetMonth() const { return month; }
35 Int_t GetYear() const { return year; }
36 void SetDay(Int_t d) { day=d; }
37 void SetMonth(Int_t m) { month=m;}
38 void SetYear(Int_t y) { year=y;}
39private:
40 Int_t day;
41 Int_t month;
42 Int_t year;
43};
44
45class Address {
46public:
47 Address() { }
48 Address(TString s, TString p, TString c) :
49 street(s), postalCode(p), country(c) { }
50 TString GetStreet() const { return street; }
51 TString GetPostalCode() const { return postalCode; }
52 TString GetCountry() const { return country; }
53 void SetStreet(const TString &s) { street = s; }
54 void SetPostalCode(const TString &p) { postalCode = p; }
55 void SetCountry(const TString &c) { country = c; }
56private:
57 TString street;
58 TString postalCode;
59 TString country;
60};
61
62class Person : public TObject {
63public:
64 Person() { }
65 Person(Int_t i, TString f, TString l, Char_t g, Date * d, Address * a) :
66 id(i), firstName(f), lastName(l), gender(g), dateOfBirth(d), address(a){ }
67
68 ~Person() {
69 delete dateOfBirth;
70 delete address;
71 }
72
73 TString GetFirstName() const { return firstName; }
74 TString GetLastName() const { return lastName; }
75 Char_t GetGender() const { return gender; }
76 Date *GetDate() const { return dateOfBirth; }
77 Address *GetAddress() const { return address; }
78 Int_t GetID() const { return id; }
79
80 friend ostream & operator << (ostream& out, const Person& p) {
81 out << "ID: " << p.id << endl;
82 out << "First name: " << p.firstName << endl;
83 out << "Last name: " << p.lastName << endl;
84 out << "Sex: " << p.gender << endl;
85 out << "Date of birth: " << p.dateOfBirth->GetDay() << "/"
86 << p.dateOfBirth->GetMonth() << "/"
87 << p.dateOfBirth->GetYear() << endl;
88 out << "Address: " << p.address->GetStreet() << endl;
89 out << "\t" << p.address->GetPostalCode() << endl;
90 out << "\t" << p.address->GetCountry() << endl;
91 out << endl;
92 return out;
93 }
94
95private:
96 Int_t id;
97 TString firstName;
98 TString lastName;
99 Char_t gender;
100 Date *dateOfBirth;
101 Address *address;
102};
103
104class PersonList {
105public:
106 PersonList() {
107 listOfPerson = new TList();
108 }
109
110 Int_t ParseFile(TString filename) {
111 TDOMParser *domParser = new TDOMParser();
112 Int_t parsecode = domParser->ParseFile(filename);
113
114 if (parsecode < 0) {
115 cerr << domParser->GetParseCodeMessage(parsecode) << endl;
116 return -1;
117 }
118
119 TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
120
121 ParsePersonList(node);
122
123 return 0;
124 }
125
126 void ParsePersonList(TXMLNode *node) {
127 for (; node; node = node->GetNextNode()) {
128 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
129 if (strcmp(node->GetNodeName(), "Person") == 0) {
130 Int_t id=0;
131 if (node->HasAttributes()) {
132 TList *attrList = node->GetAttributes();
133 TXMLAttr *attr = 0;
134 TIter next(attrList);
135 while ((attr=(TXMLAttr*)next())) {
136 if (strcmp(attr->GetName(), "ID") == 0) {
137 id = atoi(attr->GetValue());
138 break;
139 }
140 }
141 }
142 listOfPerson->Add(ParsePerson(node->GetChildren(), id));
143 }
144 }
145 ParsePersonList(node->GetChildren());
146 }
147 }
148
149 Date *ParseDate(TXMLNode *node) {
150 Int_t d=0, m=0, y=0;
151 for ( ; node; node = node->GetNextNode()) {
152 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
153 if (strcmp(node->GetNodeName(), "Day") == 0) {
154 d = atoi(node->GetText());
155 }
156 if (strcmp(node->GetNodeName(), "Month") == 0) {
157 m = atoi(node->GetText());
158 }
159 if (strcmp(node->GetNodeName(), "Year") == 0) {
160 y = atoi(node->GetText());
161 }
162 }
163 }
164 return new Date(d, m, y);
165 }
166
167 Address *ParseAddress(TXMLNode *node) {
168 TString s, p, c;
169 for( ; node!=NULL; node = node->GetNextNode()){
170 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
171 if (strcmp(node->GetNodeName(), "Street") == 0) {
172 s = node->GetText();
173 }
174 if (strcmp(node->GetNodeName(), "PostalCode") == 0) {
175 p = node->GetText();
176 }
177 if (strcmp(node->GetNodeName(), "Country") == 0) {
178 c = node->GetText();
179 }
180 }
181 }
182 return new Address(s, p, c);
183 }
184
185 Person *ParsePerson(TXMLNode *node, Int_t id) {
186 TString firstName, lastName;
187 char gender = ' ';
188 Date *date;
189 Address *address;
190
191 for ( ; node; node = node->GetNextNode()) {
192 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
193 if (strcmp(node->GetNodeName(), "FirstName") == 0)
194 firstName = node->GetText();
195 if (strcmp(node->GetNodeName(), "LastName") == 0)
196 lastName = node->GetText();
197 if (strcmp(node->GetNodeName(), "Gender") == 0)
198 gender = node->GetText()[0];
199 if (strcmp(node->GetNodeName(), "DateOfBirth") == 0)
200 date = ParseDate(node->GetChildren());
201 if (strcmp(node->GetNodeName(), "Address") == 0)
202 address = ParseAddress(node->GetChildren());
203 }
204 }
205
206 return new Person(id, firstName, lastName, gender, date, address);
207 }
208
209 friend ostream& operator << (ostream& out, const PersonList & pl) {
210 TIter next(pl.listOfPerson);
211 Person *p;
212 while ((p =(Person*)next())){
213 out << *p << endl;
214 }
215 return out;
216 }
217
218 void PrintPerson() {
219 TIter next(listOfPerson);
220 Person *p;
221 while ((p =(Person*)next())) {
222 cout << *p << endl;
223 }
224 }
225
226private:
227 Int_t numberOfPersons;
228 TList *listOfPerson;
229};
230
231
232void DOMParsePerson()
233{
234 PersonList personlist;
235 gROOT->ProcessLine(".O 0");
236 TString dir = gROOT->GetTutorialDir();
237 if (personlist.ParseFile(dir+"/xml/person.xml") == 0)
238 cout << personlist << endl;
239}
#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:406
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