// Read XML via a DOM
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.FactoryConfigurationError;  
import javax.xml.parsers.ParserConfigurationException;
 
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;  

import java.io.File;
import java.io.IOException;

import org.w3c.dom.*;


public class DomDemo{
    static Document document; 
    static int level=0;

// convert a node type to a string
    public static String nodeType(Node node) {
        switch (node.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            return "Attribute";
        case Node.CDATA_SECTION_NODE:
            return "CDATA Section";
        case Node.COMMENT_NODE:
            return "Comment";
        case Node.DOCUMENT_FRAGMENT_NODE:
            return "Doc Fragment";
        case Node.DOCUMENT_NODE:
            return "Document";
        case Node.DOCUMENT_TYPE_NODE:
            return "Document Type";
        case Node.ELEMENT_NODE:
            return "Element";
        case Node.ENTITY_NODE:
            return "Entity";
        case Node.ENTITY_REFERENCE_NODE:
            return "Entity Reference";
        case Node.NOTATION_NODE:
            return "Notation";
        case Node.PROCESSING_INSTRUCTION_NODE:
            return "Processing Instruction";
        case Node.TEXT_NODE:
            return "Text";
        default:
            return "Unknown";
        }
    }

// recursively walk the nodes
    public static void walk(Node node) {
        NodeList nodes=node.getChildNodes();
        String val;
        NamedNodeMap attr=null;
        if (node.getNodeType()==Node.TEXT_NODE || 
            node.getNodeType()==Node.ATTRIBUTE_NODE) {
            val=node.getNodeValue().trim();
            if (val.length()==0) return; // don't print pure white space
        }
        else {
            val=node.getNodeName();
            attr=node.getAttributes();
        }
        for (int j=0;j<level;j++) System.out.print("\t");
        System.out.print("Level " + level+1);
        System.out.println(" " + nodeType(node) + ":" + val);
        if (attr!=null){
            for (int j=0;j<attr.getLength();j++)
                walk(attr.item(j));                
        }
        level++;
        for (int i=0;i<nodes.getLength();i++)
            walk(nodes.item(i));
        level--;
    }

    public static void main(String argv[]) throws Exception
    {
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
// This one line loads the whole document!
           document = builder.parse( new File(argv[0]) );
           walk(document);
 
        } catch (SAXException sxe) {
           // Error generated during parsing)
           Exception  x = sxe;
           if (sxe.getException() != null)
               x = sxe.getException();
           x.printStackTrace();

        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            pce.printStackTrace();

        } catch (IOException ioe) {
           // I/O error
           ioe.printStackTrace();
        }
    } // main

}
