Logo ROOT   6.07/09
Reference Guide
DOMRecursive.C File Reference

Detailed Description

View in nbviewer Open in SWAN ROOT implementation of a XML DOM Parser

This is an example of how Dom Parser walks the DOM tree recursively. This example will parse any xml file.

To run this program

.x DOMRecursive.C+

Requires: person.xml

0.0240118503571
4.33389282227
Processing /mnt/vdb/lsf/workspace/root-makedoc-v608/rootspi/rdoc/src/v6-08-00-patches/tutorials/xml/DOMRecursive.C...
PersonList:
Comment:
This is an example...
Person: ID:1
FirstName: Alicia
LastName: Smith
Gender: F
DateOfBirth:
Day: 13
Month: 10
Year: 1978
Address:
Street: Grand Avenue, 143
PostalCode: Toronto 2283
Country: Canada
Person: ID:2
FirstName: Maria
LastName: White
Gender: F
DateOfBirth:
Day: 29
Month: 5
Year: 1980
Address:
Street: Green Land Park, 143
PostalCode: Vancouver BC V6C 2C2
Country: Canada
#include <Riostream.h>
#include <TDOMParser.h>
#include <TXMLNode.h>
#include <TXMLAttr.h>
#include <TList.h>
void ParseContext(TXMLNode *node)
{
for ( ; node; node = node->GetNextNode()) {
if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
cout << node->GetNodeName() << ": ";
if (node->HasAttributes()) {
TList* attrList = node->GetAttributes();
TIter next(attrList);
TXMLAttr *attr;
while ((attr =(TXMLAttr*)next())) {
cout << attr->GetName() << ":" << attr->GetValue();
}
}
}
if (node->GetNodeType() == TXMLNode::kXMLTextNode) { // Text node
cout << node->GetContent();
}
if (node->GetNodeType() == TXMLNode::kXMLCommentNode) { //Comment node
cout << "Comment: " << node->GetContent();
}
ParseContext(node->GetChildren());
}
}
void DOMRecursive()
{
TDOMParser *domParser = new TDOMParser();
TString dir = gROOT->GetTutorialsDir();
domParser->SetValidate(false); // do not validate with DTD
domParser->ParseFile(dir+"/xml/person.xml");
TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
ParseContext(node);
}
Author
Sergey Linev

Definition in file DOMRecursive.C.