XML DOM tutoria...login
XML DOM tutorial
author:php.cn  update time:2022-04-13 15:27:56

DOM Element


XML DOM - Element Object


Element Object

The Element object represents an element in an XML document. Elements can contain attributes, other elements, or text. If an element contains text, that text is represented in a text node.

Important: Text is always stored in text nodes. A common mistake during DOM processing is to navigate to an element node and assume that this node contains text. However, even the simplest element nodes have text nodes underneath them. For example, in <year>2005</year>, there is an element node (year), and there is a text node under this node, which contains the text (2005).

Since the Element object is also a node, it can inherit the properties and methods of the Node object.

Element object attributes

AttributesDescription
attributesReturn the NamedNodeMap of the element's attributes.
baseURIReturns the absolute base URI of the element.
childNodesReturns a NodeList of child nodes of the element.
firstChild Returns the first child node of the element.
lastChildReturns the last child node of the element.
localName Returns the local part of the element name.
namespaceURIReturns the namespace URI of the element.
nextSiblingReturns the node immediately after the element.
nodeName Returns the name of the node, according to its type.
nodeTypeReturns the type of node.
ownerDocumentReturns the root element (document object) to which the element belongs.
parentNodeReturns the parent node of the element.
prefix Sets or returns the namespace prefix of the element.
previousSiblingReturns the node immediately before the element.
schemaTypeInfoReturns the type information associated with the element.
tagNameReturns the name of the element.
textContentSet or return the text content of the element and its descendants.

Element Object Method

MethodDescription
appendChild() Adds the new child node to the end of the node's child node list.
cloneNode()Clone node.
compareDocumentPosition()Compare the document positions of two nodes.
getAttribute()Returns the value of the attribute.
getAttributeNS()Returns the value of the attribute (with namespace).
getAttributeNode()Returns the attribute node as an Attribute object.
getAttributeNodeNS()Returns the attribute node (with namespace) as an Attribute object.
getElementsByTagName()Returns a NodeList of matching element nodes and their child nodes.
getElementsByTagNameNS() Returns a NodeList of matching element nodes (with namespaces) and their child nodes.
getFeature(feature,version)Returns a DOM object that can execute a specialized API with the specified feature and version.
getUserData(key)Returns the object associated with the key on the node. This object must first be set to this node by calling setUserData with the same key.
hasAttribute()Returns whether the element has an attribute matching the specified name.
hasAttributeNS()Returns whether the element has an attribute matching the specified name and namespace.
hasAttributes()Returns whether the element has attributes.
hasChildNodes()Returns whether the element has child nodes.
insertBefore()Insert a new child node before the existing child node.
isDefaultNamespace(URI)Returns whether the specified namespaceURI is the default.
isEqualNode() Checks whether two nodes are equal.
isSameNode() Check whether two nodes are the same node.
isSupported(feature,version)Returns whether the specified feature is supported on this element.
lookupNamespaceURI()Returns the namespace URI matching the specified prefix.
lookupPrefix()Returns the prefix that matches the specified namespace URI.
normalize()Place all text nodes under the node (including attributes) into a "standard" format, in which only structures (such as elements, comments , processing instructions, CDATA sections, and entity references) to separate Text nodes, for example, there are neither adjacent Text nodes nor empty Text nodes.
removeAttribute()Remove the specified attribute.
removeAttributeNS()Remove the specified attribute (with namespace).
removeAttributeNode()Delete the specified attribute node.
removeChild()Delete child nodes.
replaceChild()Replace child node.
setUserData(key,data,handler)Associate the object to the key on the element.
setAttribute()Add new attributes.
setAttributeNS()Add new attributes (with namespace).
setAttributeNode()Add a new attribute node.
setAttributeNodeNS(attrnode)Add a new attribute node (with namespace).
setIdAttribute(name,isId)If the isId attribute of the Attribute object is true, then this method will declare the specified attribute as an attribute with a user-determined ID ( user-determined ID attribute).
setIdAttributeNS(uri,name,isId)If the isId attribute of the Attribute object is true, then this method will declare the specified attribute as a user-determined ID Attribute (user-determined ID attribute) (with namespace).
setIdAttributeNode(idAttr,isId)If the isId attribute of the Attribute object is true, then this method will declare the specified attribute as an attribute with a user-determined ID ( user-determined ID attribute).

php.cn