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

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:
};
class Address {
public:
Address() { }
Address(TString s, TString p, TString 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:
};
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:
Address *address;
};
class PersonList {
public:
}
Int_t ParseFile(TString filename) {
if (parsecode < 0) {
cerr << domParser->GetParseCodeMessage(parsecode) << endl;
return -1;
}
TXMLNode * node = domParser->GetXMLDocument()->GetRootNode();
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()) {
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));
}
}
}
}
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);
}
char gender = ' ';
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)
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);
while ((p =(Person*)next())){
out << *p << endl;
}
return out;
}
void PrintPerson() {
while ((p =(Person*)next())) {
cout << *p << endl;
}
}
private:
};
{
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
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:397
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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 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
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.