Logo ROOT  
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

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->GetTutorialDir();
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.

TXMLNode::GetContent
const char * GetContent() const
Returns the content if any, or 0.
Definition: TXMLNode.cxx:97
TXMLNode::GetChildren
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition: TXMLNode.cxx:74
TDOMParser.h
TXMLAttr
Definition: TXMLAttr.h:18
TXMLAttr::GetValue
const char * GetValue() const
Definition: TXMLAttr.h:39
TXMLNode.h
TList.h
TXMLDocument::GetRootNode
TXMLNode * GetRootNode() const
Returns the root element node.
Definition: TXMLDocument.cxx:53
TString
Definition: TString.h:136
TDOMParser::GetXMLDocument
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Definition: TDOMParser.cxx:144
TXMLNode
Definition: TXMLNode.h:20
TXMLNode::kXMLCommentNode
@ kXMLCommentNode
Definition: TXMLNode.h:40
TXMLNode::kXMLElementNode
@ kXMLElementNode
Definition: TXMLNode.h:37
TXMLNode::HasAttributes
Bool_t HasAttributes() const
Returns true if Element node has attribute.
Definition: TXMLNode.cxx:198
TXMLAttr.h
TDOMParser
Definition: TDOMParser.h:20
TXMLAttr::GetName
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:37
TXMLNode::GetNextNode
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition: TXMLNode.cxx:130
TXMLNode::kXMLTextNode
@ kXMLTextNode
Definition: TXMLNode.h:39
TIter
Definition: TCollection.h:233
TXMLNode::GetNodeName
const char * GetNodeName() const
Returns the node's name.
Definition: TXMLNode.cxx:66
TXMLParser::SetValidate
void SetValidate(Bool_t val=kTRUE)
The parser will validate the xml file if val = true.
Definition: TXMLParser.cxx:76
Riostream.h
TXMLNode::GetAttributes
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition: TXMLNode.cxx:108
TXMLNode::GetNodeType
EXMLElementType GetNodeType() const
Returns the node's type.
Definition: TXMLNode.cxx:58
TDOMParser::ParseFile
virtual Int_t ParseFile(const char *filename)
Parse the XML file where filename is the XML file name.
Definition: TDOMParser.cxx:70
TList
Definition: TList.h:44
gROOT
#define gROOT
Definition: TROOT.h:406