The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > org.w3c.dom  [30 examples] > Getting Nodes  [5 examples]

e527. Getting a Node Relative to Another Node in a DOM Document

This example demonstrates the various methods for using a node to retrieve related nodes --- parent, child, etc.
    // Get the parent
    Node parent = node.getParentNode();
    
    // Get children
    NodeList children = node.getChildNodes();
    
    // Get first child; null if no children
    Node child = node.getFirstChild();
    
    // Get last child; null if no children
    child = node.getLastChild();
    
    // Get next sibling; null if node is last child
    Node sibling = node.getNextSibling();
    
    // Get previous sibling; null if node is first child
    sibling = node.getPreviousSibling();
    
    // Get first sibling
    sibling = node.getParentNode().getFirstChild();
    
    // Get last sibling
    sibling = node.getParentNode().getLastChild();

 Related Examples
e1073. Getting the Root Element in a DOM Document
e528. Getting the Notations in a DOM Document
e529. Getting the Declared Entities in a DOM Document
e530. Getting the Value of an Entity Reference in a DOM Document

See also: Adding and Removing Nodes    Element Attributes    Elements    Text Nodes    XPath   


© 2002 Addison-Wesley.