ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DOMRecursive.C
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // ROOT implementation of a XML DOM Parser
4 //
5 // This is an example of how Dom Parser walks the DOM tree recursively.
6 // This example will parse any xml file.
7 //
8 // To run this program
9 // .x DOMRecursive.C+
10 //
11 // Requires: person.xml
12 //
13 //////////////////////////////////////////////////////////////////////////////
14 
15 #include <Riostream.h>
16 #include <TDOMParser.h>
17 #include <TXMLNode.h>
18 #include <TXMLAttr.h>
19 #include <TList.h>
20 
21 
23 {
24  for ( ; node; node = node->GetNextNode()) {
25  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
26  cout << node->GetNodeName() << ": ";
27  if (node->HasAttributes()) {
28  TList* attrList = node->GetAttributes();
29  TIter next(attrList);
30  TXMLAttr *attr;
31  while ((attr =(TXMLAttr*)next())) {
32  cout << attr->GetName() << ":" << attr->GetValue();
33  }
34  }
35  }
36  if (node->GetNodeType() == TXMLNode::kXMLTextNode) { // Text node
37  cout << node->GetContent();
38  }
39  if (node->GetNodeType() == TXMLNode::kXMLCommentNode) { //Comment node
40  cout << "Comment: " << node->GetContent();
41  }
42 
43  ParseContext(node->GetChildren());
44  }
45 }
46 
47 
49 {
50  TDOMParser *domParser = new TDOMParser();
51  TString dir = gSystem->DirName(__FILE__);
52  domParser->SetValidate(false); // do not validate with DTD
53  domParser->ParseFile(dir+"/person.xml");
54 
55  TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
56 
57  ParseContext(node);
58 }
const char * GetNodeName() const
Returns the node's name.
Definition: TXMLNode.cxx:66
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
Basic string class.
Definition: TString.h:137
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:980
TXMLNode * GetRootNode() const
Returns the root element node.
void SetValidate(Bool_t val=kTRUE)
The parser will validate the xml file if val = true.
Definition: TXMLParser.cxx:77
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Definition: TDOMParser.cxx:144
Bool_t HasAttributes() const
Returns true if Element node has attribute.
Definition: TXMLNode.cxx:198
const char * GetContent() const
Returns the content if any, or 0.
Definition: TXMLNode.cxx:97
A doubly linked list.
Definition: TList.h:47
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition: TXMLNode.cxx:130
virtual Int_t ParseFile(const char *filename)
Parse the XML file where filename is the XML file name.
Definition: TDOMParser.cxx:70
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition: TXMLNode.cxx:108
TXMLAttribute is the attribute of an Element.
Definition: TXMLAttr.h:20
const char * GetValue() const
Definition: TXMLAttr.h:35
EXMLElementType GetNodeType() const
Returns the node's type.
Definition: TXMLNode.cxx:58
void dir(char *path=0)
Definition: rootalias.C:30
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:33
void DOMRecursive()
Definition: DOMRecursive.C:48
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
Definition: TXMLNode.h:26
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition: TXMLNode.cxx:74
void ParseContext(TXMLNode *node)
Definition: DOMRecursive.C:22