2008-11-05
Tyng-Ruey Chuang
trc@iis.sinica.edu.tw
Institute of Information Science
Academia Sinica, Taipei, Taiwan
Node
objects that
also implement other, more specialized interfaces. Some types of
nodes may have child nodes of various types,
and others are leaf nodes that cannot have
anything below them in the document structure.NodeList
interface
to handle ordered lists of Nodes
, such as the
children of a Node
, or the elements returned by
the getElementsByTagName
method of the Element
interface,
and also a NamedNodeMap
interface to handle unordered
sets of nodes referenced by their name attribute,
such as the attributes of an Element
.NodeList
and NamedNodeMap
objects in the DOM are live; that is, changes to the
underlying document structure are reflected in all relevant
NodeList
and NamedNodeMap
objects.The node types, and which node types they may have as children, are as follows:
Document
— Element
(maximum of
one), ProcessingInstruction
, Comment
,
DocumentType
(maximum of one)DocumentFragment
— Element
,
ProcessingInstruction
, Comment
,
Text
, CDATASection
, EntityReference
DocumentType
— no childrenEntityReference
— Element
,
ProcessingInstruction
, Comment
,
Text
, CDATASection
, EntityReference
Element
— Element
,
Text
, Comment
, ProcessingInstruction
,
CDATASection
, EntityReference
Attr
— Text
, EntityReference
ProcessingInstruction
— no childrenComment
— no childrenText
— no childrenCDATASection
— no childrenEntity
— Element
, ProcessingInstruction
,
Comment
, Text
, CDATASection
,
EntityReference
Notation
— no childrenNode
The Node
interface is the primary datatype for the
entire Document Object Model. It represents a single node in the
document tree. While all objects implementing the Node
interface expose methods for dealing with children, not all objects
implementing the Node
interface may have children.
The attributes nodeName
, nodeValue
and
attributes
are included as a mechanism to get at node
information without casting down to the specific derived interface.
In cases where there is no obvious mapping of these attributes for
a specific nodeType
(e.g., nodeValue
for
an Element
or attributes
for a Comment
), this
returns null
. Note that the specialized interfaces may
contain additional and more convenient mechanisms to get and set
the relevant information.
interface Node { // NodeType const unsigned short ELEMENT_NODE = 1; const unsigned short ATTRIBUTE_NODE = 2; const unsigned short TEXT_NODE = 3; const unsigned short CDATA_SECTION_NODE = 4; const unsigned short ENTITY_REFERENCE_NODE = 5; const unsigned short ENTITY_NODE = 6; const unsigned short PROCESSING_INSTRUCTION_NODE = 7; const unsigned short COMMENT_NODE = 8; const unsigned short DOCUMENT_NODE = 9; const unsigned short DOCUMENT_TYPE_NODE = 10; const unsigned short DOCUMENT_FRAGMENT_NODE = 11; const unsigned short NOTATION_NODE = 12; readonly attribute DOMString nodeName; attribute DOMString nodeValue; // raises(DOMException) on setting // raises(DOMException) on retrieval readonly attribute unsigned short nodeType; readonly attribute Node parentNode; readonly attribute NodeList childNodes; readonly attribute Node firstChild; readonly attribute Node lastChild; readonly attribute Node previousSibling; readonly attribute Node nextSibling; readonly attribute NamedNodeMap attributes; // Modified in DOM Level 2: readonly attribute Document ownerDocument; Node insertBefore(in Node newChild, in Node refChild) raises(DOMException); Node replaceChild(in Node newChild, in Node oldChild) raises(DOMException); Node removeChild(in Node oldChild) raises(DOMException); Node appendChild(in Node newChild) raises(DOMException); boolean hasChildNodes(); Node cloneNode(in boolean deep); // Modified in DOM Level 2: void normalize(); // Introduced in DOM Level 2: boolean isSupported(in DOMString feature, in DOMString version); // Introduced in DOM Level 2: readonly attribute DOMString namespaceURI; // Introduced in DOM Level 2: attribute DOMString prefix; // raises(DOMException) on setting // Introduced in DOM Level 2: readonly attribute DOMString localName; // Introduced in DOM Level 2: boolean hasAttributes(); };
ELement
The Element
interface represents an element
in an HTML or XML document. Elements may have attributes associated with them;
since the Element
interface inherits from Node
, the generic
Node
interface attribute attributes
may be used to retrieve the set
of all attributes for an element.
interface Element : Node { readonly attribute DOMString tagName; DOMString getAttribute(in DOMString name); void setAttribute(in DOMString name, in DOMString value) raises(DOMException); void removeAttribute(in DOMString name) raises(DOMException); Attr getAttributeNode(in DOMString name); Attr setAttributeNode(in Attr newAttr) raises(DOMException); Attr removeAttributeNode(in Attr oldAttr) raises(DOMException); NodeList getElementsByTagName(in DOMString name); // Introduced in DOM Level 2: DOMString getAttributeNS(in DOMString namespaceURI, in DOMString localName); // Introduced in DOM Level 2: void setAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName, in DOMString value) raises(DOMException); // Introduced in DOM Level 2: void removeAttributeNS(in DOMString namespaceURI, in DOMString localName) raises(DOMException); // Introduced in DOM Level 2: Attr getAttributeNodeNS(in DOMString namespaceURI, in DOMString localName); // Introduced in DOM Level 2: Attr setAttributeNodeNS(in Attr newAttr) raises(DOMException); // Introduced in DOM Level 2: NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName); // Introduced in DOM Level 2: boolean hasAttribute(in DOMString name); // Introduced in DOM Level 2: boolean hasAttributeNS(in DOMString namespaceURI, in DOMString localName); };
Node
package org.w3c.dom; public interface Node { // NodeType public static final short ELEMENT_NODE = 1; public static final short ATTRIBUTE_NODE = 2; public static final short TEXT_NODE = 3; public static final short CDATA_SECTION_NODE = 4; public static final short ENTITY_REFERENCE_NODE = 5; public static final short ENTITY_NODE = 6; public static final short PROCESSING_INSTRUCTION_NODE = 7; public static final short COMMENT_NODE = 8; public static final short DOCUMENT_NODE = 9; public static final short DOCUMENT_TYPE_NODE = 10; public static final short DOCUMENT_FRAGMENT_NODE = 11; public static final short NOTATION_NODE = 12; public String getNodeName(); public String getNodeValue() throws DOMException; public void setNodeValue(String nodeValue) throws DOMException; public short getNodeType(); public Node getParentNode(); public NodeList getChildNodes(); public Node getFirstChild(); public Node getLastChild(); public Node getPreviousSibling(); public Node getNextSibling(); public NamedNodeMap getAttributes(); public Document getOwnerDocument(); public Node insertBefore(Node newChild, Node refChild) throws DOMException; public Node replaceChild(Node newChild, Node oldChild) throws DOMException; public Node removeChild(Node oldChild) throws DOMException; public Node appendChild(Node newChild) throws DOMException; public boolean hasChildNodes(); public Node cloneNode(boolean deep); public void normalize(); public boolean isSupported(String feature, String version); public String getNamespaceURI(); public String getPrefix(); public void setPrefix(String prefix) throws DOMException; public String getLocalName(); public boolean hasAttributes(); }
Element
package org.w3c.dom; public interface Element extends Node { public String getTagName(); public String getAttribute(String name); public void setAttribute(String name, String value) throws DOMException; public void removeAttribute(String name) throws DOMException; public Attr getAttributeNode(String name); public Attr setAttributeNode(Attr newAttr) throws DOMException; public Attr removeAttributeNode(Attr oldAttr) throws DOMException; public NodeList getElementsByTagName(String name); public String getAttributeNS(String namespaceURI, String localName); public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException; public void removeAttributeNS(String namespaceURI, String localName) throws DOMException; public Attr getAttributeNodeNS(String namespaceURI, String localName); public Attr setAttributeNodeNS(Attr newAttr) throws DOMException; public NodeList getElementsByTagNameNS(String namespaceURI, String localName); public boolean hasAttribute(String name); public boolean hasAttributeNS(String namespaceURI, String localName); }