Coverage Report

Created: 2026-06-07 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/XML/include/Poco/DOM/Element.h
Line
Count
Source
1
//
2
// Element.h
3
//
4
// Library: XML
5
// Package: DOM
6
// Module:  DOM
7
//
8
// Definition of the DOM Element class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef DOM_Element_INCLUDED
18
#define DOM_Element_INCLUDED
19
20
21
#include "Poco/XML/XML.h"
22
#include "Poco/DOM/AbstractContainerNode.h"
23
#include "Poco/XML/Name.h"
24
25
26
namespace Poco::XML {
27
28
29
class Attr;
30
class NodeList;
31
class Document;
32
33
34
class XML_API Element: public AbstractContainerNode
35
  /// The Element interface represents an element in an XML document.
36
  /// Elements may have attributes associated with them; since the Element interface
37
  /// inherits from Node, the generic Node interface attribute attributes may
38
  /// be used to retrieve the set of all attributes for an element. There are
39
  /// methods on the Element interface to retrieve either an Attr object by name
40
  /// or an attribute value by name. In XML, where an attribute value may contain
41
  /// entity references, an Attr object should be retrieved to examine the possibly
42
  /// fairly complex sub-tree representing the attribute value.
43
{
44
public:
45
  const XMLString& tagName() const;
46
    /// Returns the name of the element.
47
    ///
48
    /// For example, in
49
    ///
50
    ///     <elementExample id="demo">
51
    ///         ...
52
    ///     </elementExample>
53
    ///
54
    /// tagName has the value "elementExample". Note that this is case-preserving in XML,
55
    /// as are all of the operations of the DOM.
56
57
  const XMLString& getAttribute(const XMLString& name) const;
58
    /// Retrieves an attribute value by name.
59
    ///
60
    /// Returns the attribute's value, if the attribute
61
    /// exists, or an empty string otherwise.
62
63
  void setAttribute(const XMLString& name, const XMLString& value);
64
    /// Adds a new attribute. If an attribute with that name is already present
65
    /// in the element, its value is changed to be that of the value parameter.
66
    /// This value is a simple string; it is not parsed as it is being set. So any
67
    /// markup (such as syntax to be recognized as an entity reference) is treated
68
    /// as literal text, and needs to be appropriately escaped by the implementation
69
    /// when it is written out.
70
71
  void removeAttribute(const XMLString& name);
72
    /// Removes an attribute by name.
73
74
  Attr* getAttributeNode(const XMLString& name) const;
75
    /// Retrieves an Attr node by name.
76
77
  Attr* setAttributeNode(Attr* newAttr);
78
    /// Adds a new attribute. If an attribute with that name is already
79
    /// present in the element, it is replaced by the new one.
80
81
  Attr* addAttributeNodeNP(Attr* oldAttr, Attr* newAttr);
82
    /// For internal use only.
83
    /// Adds a new attribute after oldAttr.
84
    /// If oldAttr is 0, newAttr is set as first attribute.
85
    /// Returns newAttr.
86
    /// Does not fire any events.
87
88
  Attr* removeAttributeNode(Attr* oldAttr);
89
    /// Removes the specified attribute.
90
91
  NodeList* getElementsByTagName(const XMLString& name) const;
92
    /// Returns a NodeList of all descendant elements with a given tag
93
    /// name, in the order in which they would be encountered in a
94
    /// preorder traversal of the Element tree.
95
    ///
96
    /// The special name "*" matches all tags.
97
    ///
98
    /// The returned NodeList must be released with a call
99
    /// to release() when no longer needed.
100
101
  void normalize();
102
    /// Puts all Text nodes in the full depth of the sub-tree underneath this Element,
103
    /// including attribute nodes, into a "normal" form where only markup (e.g.,
104
    /// tags, comments, processing instructions, CDATA sections, and entity references)
105
    /// separates Text nodes, i.e., there are no adjacent Text nodes. This can be
106
    /// used to ensure that the DOM view of a document is the same as if it were
107
    /// saved and re-loaded, and is useful when operations (such as XPointer
108
    /// lookups) that depend on a particular document tree structure are to be used.
109
    ///
110
    /// Note: In cases where the document contains CDATASections, the normalize
111
    /// operation alone may not be sufficient, since XPointers do not differentiate
112
    /// between Text nodes and CDATASection nodes.
113
114
  // DOM Level 2
115
  const XMLString& getAttributeNS(const XMLString& namespaceURI, const XMLString& localName) const;
116
    /// Retrieves an attribute value by name.
117
    ///
118
    /// Returns the attribute's value, if the attribute
119
    /// exists, or an empty string otherwise.
120
121
  void setAttributeNS(const XMLString& namespaceURI, const XMLString& qualifiedName, const XMLString& value);
122
    /// Adds a new attribute. If an attribute with that name
123
    /// is already present in the element, its value is changed
124
    /// to be that of the value parameter.
125
126
  void removeAttributeNS(const XMLString& namespaceURI, const XMLString& localName);
127
    /// Removes an attribute by name.
128
129
  Attr* getAttributeNodeNS(const XMLString& namespaceURI, const XMLString& localName) const;
130
    /// Retrieves an Attr node by name.
131
132
  Attr* setAttributeNodeNS(Attr* newAttr);
133
    /// Adds a new attribute. If an attribute with that name is already
134
    /// present in the element, it is replaced by the new one.
135
136
  bool hasAttribute(const XMLString& name) const;
137
    /// Returns true if and only if the element has the specified attribute.
138
139
  bool hasAttributeNS(const XMLString& namespaceURI, const XMLString& localName) const;
140
    /// Returns true if and only if the element has the specified attribute.
141
142
  NodeList* getElementsByTagNameNS(const XMLString& namespaceURI, const XMLString& localName) const;
143
    /// Returns a NodeList of all the descendant Elements with a given local name and namespace URI
144
    /// in the order in which they are encountered in a preorder traversal of this Element tree.
145
    ///
146
    /// The special value "*" matches all namespaces, or local names respectively.
147
    ///
148
    /// The returned NodeList must be released with a call
149
    /// to release() when no longer needed.
150
151
  const XMLString& namespaceURI() const;
152
  XMLString prefix() const;
153
  const XMLString& localName() const;
154
  bool hasAttributes() const;
155
  XMLString innerText() const;
156
157
  Element* getChildElement(const XMLString& name) const;
158
    /// Returns the first child element with the given name, or null
159
    /// if such an element does not exist.
160
    ///
161
    /// This method is an extension to the W3C Document Object Model.
162
163
  Element* getChildElementNS(const XMLString& namespaceURI, const XMLString& localName) const;
164
    /// Returns the first child element with the given namespaceURI and localName,
165
    /// or null if such an element does not exist.
166
    ///
167
    /// This method is an extension to the W3C Document Object Model.
168
169
  Element* getElementById(const XMLString& elementId, const XMLString& idAttribute) const;
170
    /// Returns the first Element whose ID attribute (given in idAttribute)
171
    /// has the given elementId. If no such element exists, returns null.
172
    ///
173
    /// This method is an extension to the W3C Document Object Model.
174
175
  Element* getElementByIdNS(const XMLString& elementId, const XMLString& idAttributeURI, const XMLString& idAttributeLocalName) const;
176
    /// Returns the first Element whose ID attribute (given in idAttributeURI and idAttributeLocalName)
177
    /// has the given elementId. If no such element exists, returns null.
178
    ///
179
    /// This method is an extension to the W3C Document Object Model.
180
181
  // Node
182
  const XMLString& nodeName() const;
183
  NamedNodeMap* attributes() const;
184
  unsigned short nodeType() const;
185
186
protected:
187
  Element(Document* pOwnerDocument, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname);
188
  Element(Document* pOwnerDocument, const Element& elem);
189
  ~Element();
190
191
  Node* copyNode(bool deep, Document* pOwnerDocument) const;
192
193
  void dispatchNodeRemovedFromDocument();
194
  void dispatchNodeInsertedIntoDocument();
195
196
private:
197
  const Name& _name;
198
  Attr*       _pFirstAttr;
199
200
  friend class Attr;
201
  friend class Document;
202
  friend class AttrMap;
203
};
204
205
206
//
207
// inlines
208
//
209
inline const XMLString& Element::tagName() const
210
0
{
211
0
  return _name.qname();
212
0
}
213
214
215
} // namespace Poco::XML
216
217
218
#endif // DOM_Element_INCLUDED