Logo ROOT   6.07/09
Reference Guide
DOMRecursive.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_xml
3 /// \notebook -nodraw
4 /// ROOT implementation of a XML DOM Parser
5 ///
6 /// This is an example of how Dom Parser walks the DOM tree recursively.
7 /// This example will parse any xml file.
8 ///
9 /// To run this program
10 /// ~~~{.cpp}
11 /// .x DOMRecursive.C+
12 /// ~~~
13 ///
14 /// Requires: `person.xml`
15 ///
16 /// \macro_output
17 /// \macro_code
18 ///
19 /// \author Sergey Linev
20 
21 #include <Riostream.h>
22 #include <TDOMParser.h>
23 #include <TXMLNode.h>
24 #include <TXMLAttr.h>
25 #include <TList.h>
26 
27 
28 void ParseContext(TXMLNode *node)
29 {
30  for ( ; node; node = node->GetNextNode()) {
31  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
32  cout << node->GetNodeName() << ": ";
33  if (node->HasAttributes()) {
34  TList* attrList = node->GetAttributes();
35  TIter next(attrList);
36  TXMLAttr *attr;
37  while ((attr =(TXMLAttr*)next())) {
38  cout << attr->GetName() << ":" << attr->GetValue();
39  }
40  }
41  }
42  if (node->GetNodeType() == TXMLNode::kXMLTextNode) { // Text node
43  cout << node->GetContent();
44  }
45  if (node->GetNodeType() == TXMLNode::kXMLCommentNode) { //Comment node
46  cout << "Comment: " << node->GetContent();
47  }
48 
49  ParseContext(node->GetChildren());
50  }
51 }
52 
53 
54 void DOMRecursive()
55 {
56  TDOMParser *domParser = new TDOMParser();
57  TString dir = gROOT->GetTutorialsDir();
58  domParser->SetValidate(false); // do not validate with DTD
59  domParser->ParseFile(dir+"/xml/person.xml");
60 
61  TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
62 
63  ParseContext(node);
64 }
const char * GetNodeName() const
Returns the node&#39;s name.
Definition: TXMLNode.cxx:66
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
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
TList * GetAttributes()
Returns a list of node&#39;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&#39;s type.
Definition: TXMLNode.cxx:58
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:33
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
Definition: TXMLNode.h:26
TXMLNode * GetChildren()
Returns the node&#39;s child if any, returns 0 if no child.
Definition: TXMLNode.cxx:74