2 Author : Harsh Arora (harsh.arora@cern.ch)
8 AliXMLParser is a tool to parse XML file from URL or path and store it in trees.
12 #include "AliXMLParser.h"
14 #include <Riostream.h>
27 ClassImp(AliXMLParser);
29 AliXMLParser::AliXMLParser():
41 fEntries = new TList();
43 fTreeList = new TList();
49 fEntries->SetOwner(kTRUE);
50 fVal->SetOwner(kTRUE);
51 fTreeList->SetOwner(kTRUE);
54 AliXMLParser::AliXMLParser(const AliXMLParser& obj):
65 fTreeList = obj.fTreeList;
66 fTableTag = obj.fTableTag;
67 fInsideTree = obj.fInsideTree;
68 fNumTokens = obj.fNumTokens;
69 fEntries = obj.fEntries;
71 fNumTrees = obj.fNumTrees;
75 AliXMLParser& AliXMLParser::operator=(const AliXMLParser& other)
80 fTreeList = other.fTreeList;
81 fTableTag = other.fTableTag;
82 fInsideTree = other.fInsideTree;
83 fNumTokens = other.fNumTokens;
84 fEntries = other.fEntries;
86 fNumTrees = other.fNumTrees;
87 fError = other.fError;
92 void AliXMLParser::OnStartDocument() {}
94 int AliXMLParser::GetEntryIndex(TString entry_name) //Finds the attribute index by name
97 for(i=0;i<=fNumTokens;i++)
99 if(entry_name.EqualTo(*(TString *)(fEntries->At(i))))
105 void AliXMLParser::OnStartElement(const char *name, const TList *attributes) //Stores Parsed XML in a Tree
108 TString tree_symbol = "roottreename";
112 TIter next(attributes);
116 if((attr = (TXMLAttr*) next()))
118 if((!fInsideTree) && tree_symbol.EqualTo(attr->GetName())) //If tag found, Enter a new tree
122 fTreeList->Add((TObject *)(new TTree(attr->GetValue(),attr->GetValue())));
127 curr_tree=(TTree *)(fTreeList->At(fNumTrees-1));
130 if(!(curr_tree->GetBranch(attr->GetName())))
133 TString var_type = attr->GetName();
137 fEntries->Add((TObject *)(new TString(attr->GetName())));
139 fVal->Add((TObject *)(new TString(temp)));
141 //Adding Attributes as branches of TTree
142 branch = curr_tree->Branch(attr->GetName(), (void *)(((TString *)(fVal->At(fNumTokens)))->Data()), var_type);
143 n = curr_tree->GetEntries();
146 (*(TString *)(fVal->At(fNumTokens))) = temp;
153 (*(TString *)(fVal->At(GetEntryIndex(attr->GetName())))) = attr->GetValue();
156 } while ((attr = (TXMLAttr*) next()));
157 curr_tree->Fill(); //Fill the tree with attribute values
158 for(i=0;i<=fNumTokens;i++)
160 (*(TString *)(fVal->At(i))) = temp;
168 void AliXMLParser::OnEndElement(const char *name)
170 //if tag closes, re-initialize everything for a new tree
171 if(fTableTag.EqualTo(name))
173 fInsideTree = kFALSE;
174 fEntries->Clear("nodelete");
175 fVal->Clear("nodelete");
180 void AliXMLParser::OnCharacters(const char *) {}
182 void AliXMLParser::OnComment(const char *) {}
184 void AliXMLParser::OnWarning(const char *warning)
186 cout << "Warning in XML: " << warning << endl;
189 void AliXMLParser::OnError(const char *error)
192 cerr << "Error in XML: " << error << endl;
195 void AliXMLParser::OnFatalError(const char *error)
198 cerr << "FatalError in XML: " << error << endl;
201 void AliXMLParser::OnCdataBlock(const char *, Int_t ) {}
203 void AliXMLParser::OnEndDocument() {}
205 AliXMLParser::~AliXMLParser()
213 TList* AliXMLParser::GetTreesFromXML(TString file) //Returns List of Trees by parsing local XML file
215 TSAXParser *saxParser = new TSAXParser();
216 saxParser->ConnectToHandler("AliXMLParser", this);
217 if(!TFile::Open(file))
219 cerr << "File not found" << endl;
222 saxParser->ParseFile(file);
224 if (fError) return 0x0;
228 TList *AliXMLParser::GetTreesFromURL(TString host) //Returns List of Trees by parsing remote XML file
232 TSAXParser *saxParser = new TSAXParser();
233 saxParser->ConnectToHandler("AliXMLParser", this);
234 host += "&filetype=raw";
235 TFile *file = TFile::Open(host);
238 cerr << "Invalid URL" << endl;
241 size=file->GetSize();
242 buf = (char *)malloc(size+1);
243 file->ReadBuffer(buf, size);
244 saxParser->ParseBuffer(buf,size);
247 if (fError) return 0x0;