The Java Developers Almanac 1.4


Order this book from Amazon.

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

e524. Visiting All the Nodes in a DOM Document

    // Obtain an DOM document; this method is implemented in
    // e510 The Quintessential Program to Create a DOM Document from an XML File
    Document doc = parseXmlFile("infilename.xml", true);
    
    visit(doc, 0);
    
    // This method visits all the nodes in a DOM tree
    public static void visit(Node node, int level) {
        // Process node
    
        // If there are any children, visit each one
        NodeList list = node.getChildNodes();
        for (int i=0; i<list.getLength(); i++) {
            // Get child node
            Node childNode = list.item(i);
    
            // Visit child node
            visit(childNode, level+1);
        }
    }

 Related Examples
e525. Copying a Subtree of Nodes in a DOM Document
e526. Copying a Subtree of Nodes from One DOM Document to Another

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


© 2002 Addison-Wesley.