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
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
31
32////////////////////////////////////////////////////////////////////////////////
33/// TXMLNode constructor.
34
35TXMLNode::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)
51 delete fAttrList;
52
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Returns the node's type.
57
59{
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Returns the node's name.
65
66const 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
97const 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
154const char *TXMLNode::GetText() const
155{
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
206const 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
218{
219 if (fXMLNode->ns) {
220 return (const char *) fXMLNode->ns->prefix;
221 }
222 return 0;
223}
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
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:22
TList * fAttrList
List of Attributes.
Definition: TXMLNode.h:34
TXMLNode(const TXMLNode &)
TList * GetAttributes()
Returns a list of node's attribute if any, returns 0 if no attribute.
Definition: TXMLNode.cxx:108
TXMLNode * fPreviousNode
Previous sibling node.
Definition: TXMLNode.h:33
_xmlNode * fXMLNode
libxml node
Definition: TXMLNode.h:28
Bool_t HasChildren() const
Returns true if node has children.
Definition: TXMLNode.cxx:166
virtual ~TXMLNode()
Destructor.
Definition: TXMLNode.cxx:45
Bool_t HasParent() const
Returns true if node has parent.
Definition: TXMLNode.cxx:182
const char * GetText() const
Returns the content of a Text node if node is a TextNode, 0 otherwise.
Definition: TXMLNode.cxx:154
const char * GetContent() const
Returns the content if any, or 0.
Definition: TXMLNode.cxx:97
EXMLElementType
This enum is based on libxml tree Enum xmlElementType.
Definition: TXMLNode.h:38
@ kXMLElementNode
Definition: TXMLNode.h:39
TXMLNode * fNextNode
Next sibling node.
Definition: TXMLNode.h:32
const char * GetNamespaceHref() const
Returns the URL for the namespace, or 0 if no namespace.
Definition: TXMLNode.cxx:206
const char * GetNamespacePrefix() const
Returns prefix for the namespace, or 0 if no namespace.
Definition: TXMLNode.cxx:217
TXMLNode * fParent
Parent node.
Definition: TXMLNode.h:30
TXMLNode * GetNextNode()
Returns the next sibling XMLNode in the DOM tree, if any return 0 if no next node.
Definition: TXMLNode.cxx:130
Bool_t HasPreviousNode() const
Returns true if has previous node.
Definition: TXMLNode.cxx:190
TXMLNode * GetParent() const
Returns the node's parent if any, returns 0 if no parent.
Definition: TXMLNode.cxx:89
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 * GetChildren()
Returns the node's child if any, returns 0 if no child.
Definition: TXMLNode.cxx:74
TXMLNode * fChildren
Children node.
Definition: TXMLNode.h:31
const char * GetNodeName() const
Returns the node's name.
Definition: TXMLNode.cxx:66
Bool_t HasNextNode() const
Returns true if has next node.
Definition: TXMLNode.cxx:174
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