Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DOMRecursive.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_xml
3///
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_code
17///
18/// \author Sergey Linev
19
20#include <Riostream.h>
21#include <TDOMParser.h>
22#include <TXMLNode.h>
23#include <TXMLAttr.h>
24#include <TList.h>
25
26
27void ParseContext(TXMLNode *node)
28{
29 for ( ; node; node = node->GetNextNode()) {
30 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
31 cout << node->GetNodeName() << ": ";
32 if (node->HasAttributes()) {
33 TList* attrList = node->GetAttributes();
34 TIter next(attrList);
36 while ((attr =(TXMLAttr*)next())) {
37 cout << attr->GetName() << ":" << attr->GetValue();
38 }
39 }
40 }
41 if (node->GetNodeType() == TXMLNode::kXMLTextNode) { // Text node
42 cout << node->GetContent();
43 }
44 if (node->GetNodeType() == TXMLNode::kXMLCommentNode) { //Comment node
45 cout << "Comment: " << node->GetContent();
46 }
47
48 ParseContext(node->GetChildren());
49 }
50}
51
52
53void DOMRecursive()
54{
55 TDOMParser *domParser = new TDOMParser();
56 TString dir = gROOT->GetTutorialDir();
57 domParser->SetValidate(false); // do not validate with DTD
58 domParser->ParseFile(dir+"/xml/person.xml");
59
60 TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
61
62 ParseContext(node);
63}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
#define gROOT
Definition TROOT.h:407
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Int_t ParseFile(const char *filename) override
Parse the XML file where filename is the XML file name.
A doubly linked list.
Definition TList.h:38
Basic string class.
Definition TString.h:139
TXMLAttribute is the attribute of an Element.
Definition TXMLAttr.h:18
TXMLNode * GetRootNode() const
Returns the root element node.
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
Definition TXMLNode.h:20
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition TXMLNode.cxx:108
const char * GetContent() const
Returns the content if any, or 0.
Definition TXMLNode.cxx:97
@ kXMLElementNode
Definition TXMLNode.h:37
@ kXMLCommentNode
Definition TXMLNode.h:40
@ kXMLTextNode
Definition TXMLNode.h:39
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition TXMLNode.cxx:130
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition TXMLNode.cxx:74
const char * GetNodeName() const
Returns the node's name.
Definition TXMLNode.cxx:66
Bool_t HasAttributes() const
Returns true if Element node has attribute.
Definition TXMLNode.cxx:198
EXMLElementType GetNodeType() const
Returns the node's type.
Definition TXMLNode.cxx:58
void SetValidate(Bool_t val=kTRUE)
The parser will validate the xml file if val = true.