forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMRecursive.C
More file actions
64 lines (56 loc) · 1.58 KB
/
DOMRecursive.C
File metadata and controls
64 lines (56 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// \file
/// \ingroup tutorial_xml
/// \notebook -nodraw
/// 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
/// ~~~{.cpp}
/// .x DOMRecursive.C+
/// ~~~
///
/// Requires: `person.xml`
///
/// \macro_output
/// \macro_code
///
/// \author Sergey Linev
#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);
}