Name

Node

Synopsis

All objects in a DOM document tree (including the Document object itself) implement the Node interface, which provides basic methods for traversing and manipulating the tree.

getParentNode( ) and getChildNodes( ) allow you to traverse up and down the document tree. You can enumerate the children of a given node by looping through the elements of the NodeList returned by getChildNodes( ), or by using getFirstChild( ) and getNextSibling( ) (or getLastChild( ) and getPreviousSibling( ) to loop backwards). It is sometimes useful to call hasChildNodes( ) to determine whether a node has children or not. getOwnerDocument( ) returns the Document node of which the node is a descendant or with which it is associated. It provides a quick way to jump to the root of the document tree.

Several methods allow you to add children to a tree or alter the list of children. appendChild( ) adds a new child node at the end of this nodes list of children. insertChild( ) inserts a node into this nodes list of children, placing it immediately before a specified child node. removeChild( ) removes the specified node from this node’s list of children. replaceChild( ) replaces one child node of this node with another node. For all of these methods, if the node to be appended or inserted is already part of the document tree, it is first removed from its current parent. Use cloneNode( ) to produce a copy of this node. Pass true if you want all descendants of this node to be cloned as well.

Every ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.