Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TXMLNode.cxx
Go to the documentation of this file.
1// @(#)root/xmlparser:$Id$
2// Author: Jose Lo 12/4/2005
3
4/*************************************************************************
5 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13\class TXMLNode
14\ingroup IO
15
16TXMLNode contains a pointer to xmlNode, which is a node under the
17DOM tree. A node can be an Element, an Attribute, a Text Node
18or a Comment Node.
19One can navigate the DOM tree by accessing the siblings and
20parent or child nodes. Also retriving the Attribute or the Text in
21an Element node.
22*/
23
24#include "TXMLNode.h"
25#include "TXMLAttr.h"
26#include "TList.h"
27#include <libxml/tree.h>
28
29
30
31////////////////////////////////////////////////////////////////////////////////
32/// TXMLNode constructor.
33
35 fXMLNode(node), fParent(parent), fChildren(nullptr), fNextNode(nullptr),
36 fPreviousNode(previous), fAttrList(nullptr)
37{
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Destructor. It deletes the node's child, next sibling and the
42/// attribute list.
43
45{
46 delete fChildren;
47 delete fNextNode;
48 if (fAttrList)
50 delete fAttrList;
51
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Returns the node's type.
56
61
62////////////////////////////////////////////////////////////////////////////////
63/// Returns the node's name.
64
65const char *TXMLNode::GetNodeName() const
66{
67 return (const char *) fXMLNode->name;
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// Returns the node's child if any, returns 0 if no child.
72
74{
75 if (fChildren)
76 return fChildren;
77
78 if (fXMLNode->children){
79 fChildren = new TXMLNode(fXMLNode->children, this);
80 return fChildren;
81 }
82 return nullptr;
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Returns the node's parent if any, returns 0 if no parent.
87
89{
90 return fParent;
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Returns the content if any, or 0.
95
96const char *TXMLNode::GetContent() const
97{
98 if (fXMLNode->content)
99 return (const char *) fXMLNode->content;
100 return nullptr;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Returns a list of node's attribute if any,
105/// returns 0 if no attribute.
106
108{
109 if (fAttrList)
110 return fAttrList;
111
112 if (!HasAttributes())
113 return nullptr;
114
115 fAttrList = new TList();
116 xmlAttr *attr_node = fXMLNode->properties;
117 for (; attr_node; attr_node = attr_node->next) {
118 fAttrList->Add(new TXMLAttr((const char *) attr_node->name,
119 (const char *) attr_node->children->content));
120 }
121
122 return fAttrList;
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Returns the next sibling XMLNode in the DOM tree, if any
127/// return 0 if no next node.
128
130{
131 if (fNextNode)
132 return fNextNode;
133
134 if (fXMLNode->next) {
135 fNextNode = new TXMLNode(fXMLNode->next, fParent, this);
136 return fNextNode;
137 }
138 return nullptr;
139}
140
141////////////////////////////////////////////////////////////////////////////////
142/// Returns the previous sibling XMLNode in the DOM tree, if any
143/// return 0 if no previous node
144
146{
147 return fPreviousNode;
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Returns the content of a Text node if node is a TextNode, 0 otherwise.
152
153const char *TXMLNode::GetText() const
154{
156 if (fXMLNode->children->type == XML_TEXT_NODE)
157 return (const char *) fXMLNode->children->content;
158 }
159 return nullptr;
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Returns true if node has children.
164
166{
167 return fXMLNode->children ? kTRUE : kFALSE;
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Returns true if has next node.
172
174{
175 return fXMLNode->next ? kTRUE : kFALSE;
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// Returns true if node has parent.
180
182{
183 return fXMLNode->parent ? kTRUE : kFALSE;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Returns true if has previous node.
188
190{
191 return fXMLNode->prev ? kTRUE : kFALSE;
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Returns true if Element node has attribute.
196
198{
199 return fXMLNode->properties ? kTRUE : kFALSE;
200}
201
202////////////////////////////////////////////////////////////////////////////////
203/// Returns the URL for the namespace, or 0 if no namespace.
204
205const char *TXMLNode::GetNamespaceHref() const
206{
207 if (fXMLNode->ns) {
208 return (const char *) fXMLNode->ns->href;
209 }
210 return nullptr;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Returns prefix for the namespace, or 0 if no namespace.
215
217{
218 if (fXMLNode->ns) {
219 return (const char *) fXMLNode->ns->prefix;
220 }
221 return nullptr;
222}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:467
TXMLAttribute is the attribute of an Element.
Definition TXMLAttr.h:18
TXMLNode contains a pointer to xmlNode, which is a node under the DOM tree.
Definition TXMLNode.h:20
TList * fAttrList
List of Attributes.
Definition TXMLNode.h:32
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition TXMLNode.cxx:107
TXMLNode * fPreviousNode
Previous sibling node.
Definition TXMLNode.h:31
_xmlNode * fXMLNode
libxml node
Definition TXMLNode.h:26
~TXMLNode() override
Destructor.
Definition TXMLNode.cxx:44
Bool_t HasChildren() const
Returns true if node has children.
Definition TXMLNode.cxx:165
Bool_t HasParent() const
Returns true if node has parent.
Definition TXMLNode.cxx:181
const char * GetText() const
Returns the content of a Text node if node is a TextNode, 0 otherwise.
Definition TXMLNode.cxx:153
const char * GetContent() const
Returns the content if any, or 0.
Definition TXMLNode.cxx:96
EXMLElementType
This enum is based on libxml tree Enum xmlElementType.
Definition TXMLNode.h:36
@ kXMLElementNode
Definition TXMLNode.h:37
TXMLNode * fNextNode
Next sibling node.
Definition TXMLNode.h:30
const char * GetNamespaceHref() const
Returns the URL for the namespace, or 0 if no namespace.
Definition TXMLNode.cxx:205
const char * GetNamespacePrefix() const
Returns prefix for the namespace, or 0 if no namespace.
Definition TXMLNode.cxx:216
TXMLNode * fParent
Parent node.
Definition TXMLNode.h:28
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition TXMLNode.cxx:129
Bool_t HasPreviousNode() const
Returns true if has previous node.
Definition TXMLNode.cxx:189
TXMLNode * GetParent() const
Returns the node's parent if any, returns 0 if no parent.
Definition TXMLNode.cxx:88
TXMLNode(const TXMLNode &)=delete
TXMLNode * GetPreviousNode() const
Returns the previous sibling XMLNode in the DOM tree, if any return 0 if no previous node.
Definition TXMLNode.cxx:145
TXMLNode * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition TXMLNode.cxx:73
TXMLNode * fChildren
Children node.
Definition TXMLNode.h:29
const char * GetNodeName() const
Returns the node's name.
Definition TXMLNode.cxx:65
Bool_t HasNextNode() const
Returns true if has next node.
Definition TXMLNode.cxx:173
Bool_t HasAttributes() const
Returns true if Element node has attribute.
Definition TXMLNode.cxx:197
EXMLElementType GetNodeType() const
Returns the node's type.
Definition TXMLNode.cxx:57