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