/** * This parser parses the file whose name is passed as an argument. It then * traverses the document tree, evaluates the node type and prints out a * string for the name of the node type. * * @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 Ex3NodeTypes{ 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]); System.out.println("****** Print out the short value corresponding to " + "the node type and a string describing the node type"); // 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) determine the node type and print a string for this node type // 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 = " + nodeTypeToString(node.getNodeType()) + ",\tname = " + node.getNodeName() + ",\tvalue = " + node.getNodeValue()); } // Return a string for the nodeType passed in. private static String nodeTypeToString(short nodeType){ String nodeString = "unknown"; switch (nodeType) { case Node.DOCUMENT_NODE: nodeString = "Document"; break; case Node.ELEMENT_NODE: nodeString = "Element"; break; case Node.ATTRIBUTE_NODE: nodeString = "Attribute"; break; case Node.TEXT_NODE: nodeString = "Text"; break; case Node.CDATA_SECTION_NODE: nodeString = "CDATA"; break; case Node.COMMENT_NODE: nodeString = "Comment"; break; case Node.PROCESSING_INSTRUCTION_NODE: nodeString = "Processing Instruction"; break; case Node.ENTITY_REFERENCE_NODE: nodeString = "Entity Reference"; break; case Node.DOCUMENT_TYPE_NODE: nodeString = "Document Type"; break; } return nodeString; } }