Accessing Nodes in DOM

There are two ways to get at a node in DOM. You can either walk the tree or access the node directly by name. In practice, a document is accessed using a combination of both methods.

Walking the Tree

To walk DOM tree, start anywhere in the tree and use the following methods of DOM node interface:

  • parentNode(). This method will access the parent node.

  • firstChild(). This method will access the first child of the node. If there is no such node, null is returned.

  • nextSibling(). This method accesses the next sibling node. If no such node exists, null is returned.

  • previousSibling(). This method accesses the previous sibling node. If no such node exists, null is returned.

It is easy to see how you can move throughout ...

Get XML Unleashed 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.