1XML::DOM::Element(3) User Contributed Perl Documentation XML::DOM::Element(3)
2
3
4
6 XML::DOM::Element - An XML element node in XML::DOM
7
9 XML::DOM::Element extends XML::DOM::Node.
10
11 By far the vast majority of objects (apart from text) that authors
12 encounter when traversing a document are Element nodes. Assume the
13 following XML document:
14
15 <elementExample id="demo">
16 <subelement1/>
17 <subelement2><subsubelement/></subelement2>
18 </elementExample>
19
20 When represented using DOM, the top node is an Element node for
21 "elementExample", which contains two child Element nodes, one for
22 "subelement1" and one for "subelement2". "subelement1" contains no
23 child nodes.
24
25 Elements may have attributes associated with them; since the Element
26 interface inherits from Node, the generic Node interface method
27 getAttributes may be used to retrieve the set of all attributes for an
28 element. There are methods on the Element interface to retrieve either
29 an Attr object by name or an attribute value by name. In XML, where an
30 attribute value may contain entity references, an Attr object should be
31 retrieved to examine the possibly fairly complex sub-tree representing
32 the attribute value. On the other hand, in HTML, where all attributes
33 have simple string values, methods to directly access an attribute
34 value can safely be used as a convenience.
35
36 METHODS
37 getTagName
38 The name of the element. For example, in:
39
40 <elementExample id="demo">
41 ...
42 </elementExample>
43
44 tagName has the value "elementExample". Note that this is case-
45 preserving in XML, as are all of the operations of the DOM.
46
47 getAttribute (name)
48 Retrieves an attribute value by name.
49
50 Return Value: The Attr value as a string, or the empty string if
51 that attribute does not have a specified or default value.
52
53 setAttribute (name, value)
54 Adds a new attribute. If an attribute with that name is already
55 present in the element, its value is changed to be that of the
56 value parameter. This value is a simple string, it is not parsed as
57 it is being set. So any markup (such as syntax to be recognized as
58 an entity reference) is treated as literal text, and needs to be
59 appropriately escaped by the implementation when it is written out.
60 In order to assign an attribute value that contains entity
61 references, the user must create an Attr node plus any Text and
62 EntityReference nodes, build the appropriate subtree, and use
63 setAttributeNode to assign it as the value of an attribute.
64
65 DOMExceptions:
66
67 • INVALID_CHARACTER_ERR
68
69 Raised if the specified name contains an invalid character.
70
71 • NO_MODIFICATION_ALLOWED_ERR
72
73 Raised if this node is readonly.
74
75 removeAttribute (name)
76 Removes an attribute by name. If the removed attribute has a
77 default value it is immediately replaced.
78
79 DOMExceptions:
80
81 • NO_MODIFICATION_ALLOWED_ERR
82
83 Raised if this node is readonly.
84
85 getAttributeNode
86 Retrieves an Attr node by name.
87
88 Return Value: The Attr node with the specified attribute name or
89 undef if there is no such attribute.
90
91 setAttributeNode (attr)
92 Adds a new attribute. If an attribute with that name is already
93 present in the element, it is replaced by the new one.
94
95 Return Value: If the newAttr attribute replaces an existing
96 attribute with the same name, the previously existing Attr node is
97 returned, otherwise undef is returned.
98
99 DOMExceptions:
100
101 • WRONG_DOCUMENT_ERR
102
103 Raised if newAttr was created from a different document than
104 the one that created the element.
105
106 • NO_MODIFICATION_ALLOWED_ERR
107
108 Raised if this node is readonly.
109
110 • INUSE_ATTRIBUTE_ERR
111
112 Raised if newAttr is already an attribute of another Element
113 object. The DOM user must explicitly clone Attr nodes to re-use
114 them in other elements.
115
116 removeAttributeNode (oldAttr)
117 Removes the specified attribute. If the removed Attr has a default
118 value it is immediately replaced. If the Attr already is the
119 default value, nothing happens and nothing is returned.
120
121 Parameters:
122 oldAttr The Attr node to remove from the attribute list.
123
124 Return Value: The Attr node that was removed.
125
126 DOMExceptions:
127
128 • NO_MODIFICATION_ALLOWED_ERR
129
130 Raised if this node is readonly.
131
132 • NOT_FOUND_ERR
133
134 Raised if oldAttr is not an attribute of the element.
135
136 Additional methods not in the DOM Spec
137 setTagName (newTagName)
138 Sets the tag name of the Element. Note that this method is not
139 portable between DOM implementations.
140
141 DOMExceptions:
142
143 • INVALID_CHARACTER_ERR
144
145 Raised if the specified name contains an invalid character.
146
147 check ($checker)
148 Uses the specified XML::Checker to validate the document. NOTE: an
149 XML::Checker must be supplied. The checker can be created in
150 different ways, e.g. when parsing a document with
151 XML::DOM::ValParser, or with XML::DOM::Document::createChecker().
152 See XML::Checker for more info.
153
154
155
156perl v5.36.0 2023-01-20 XML::DOM::Element(3)