/** * This parser parses the file whose name is passed as an argument. It then * traverses the document tree and prints out the node type, name and value * for the Document node as well as all the descendant nodes. The values * of attributes though are not printed out. * * @author Nauman Chaudhry * @date Apr 6, 08 */ // Note that DocumentBuilder and DocumentBuilderFactory are part // of JAXP and hence appear in the package: javax.xml.parsers import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; // Document and Node interfaces are part of DOM API and appear in org.w3c.dom import org.w3c.dom.Document; import org.w3c.dom.Node; public class Ex2TreeTraversal { public static void main(String[] args) throws Exception { // First get a document builder factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // Now get a document builder DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); return; } // Call the parse method on DocumentBuilder. This returns an object // of type Document Document myDoc = db.parse(args[0]); // Now we just print the nodeType, nodeName and nodeValue of the // document node. Look at the JavaDoc for the Node interface and // you will see that for Document node: // name = "#document" (i.e., a fixed string) // value = null // The utility of Document node is that from it we can get the DOCTYPE declaration // as well as the Root Element and then traverse the document printNode(myDoc, ""); // We now pass the Document node to a recursive procedure that will print // out all the descendant nodes process(myDoc, "\t"); } // Process all the child nodes of the Node object passed in. Processing means: // i) print out the nodeType, nodeName and nodeValue of a node // ii) call the process method with the node to process any children the node // has. private static void process(Node node, String indent) { Node c = null; // Go through each of the children one by one and call process. Note that // this code does a pre-order traversal of the tree. // You will also see that the parser considers new-lines to be Text content. // If you modify the file and remove new-line characters, you will see a // different output. for (c = node.getFirstChild(); c != null; c = c.getNextSibling()) { printNode(c, indent); process(c, indent + "\t"); } } private static void printNode(Node node, String indent){ System.out.println(indent + "node type = " + node.getNodeType() + ",\tname = " + node.getNodeName() + ",\tvalue = " + node.getNodeValue()); } }