import javax.xml.parsers.*;
import org.w3c.dom.*;

public class DOMInput {
	private int iLevel = 0;
	
	public static void main(String args[]) throws Exception {
		new DOMInput().run(args[0]);
	}
	
	private void run(String pstrURI) throws Exception {
		DocumentBuilderFactory oFactory = DocumentBuilderFactory.newInstance();
		oFactory.setValidating(false);
		DocumentBuilder oParser = oFactory.newDocumentBuilder();
		Document oDoc = oParser.parse(pstrURI);
		
		Element oRoot = oDoc.getDocumentElement();
		getTree(oRoot);
	}
	
	private void getTree(Node poNode) throws Exception {
		switch(poNode.getNodeType()){
			case Node.ELEMENT_NODE:
				doElement(poNode);
				break;
			case Node.TEXT_NODE:
			case Node.CDATA_SECTION_NODE:
				doText(poNode);
				break;
			case Node.ATTRIBUTE_NODE:
				doAttribute(poNode);
				break;
			case Node.COMMENT_NODE:
				doComment(poNode);
				break;
			case Node.PROCESSING_INSTRUCTION_NODE:
				doProcessingInstruction(poNode);
				break;
		}
	}
	
	private void doElement(Node poNode) throws Exception {
		System.out.println(getLeadingSpace(iLevel)+"Element "+poNode.getNodeName());
		NamedNodeMap oAttribs = poNode.getAttributes();
      	for (int i = 0; i<oAttribs.getLength(); i++){
      		iLevel++;
			getTree(oAttribs.item(i));
      		iLevel--;
		}
		NodeList oNodes = poNode.getChildNodes();
		for (int j=0; j<oNodes.getLength(); j++){
      		iLevel++;
			getTree(oNodes.item(j));
      		iLevel--;
		}
	}
	
	private void doText(Node poNode) throws Exception {
		System.out.println(getLeadingSpace(iLevel)+"Text: "+poNode.getNodeValue());
	}
	
	private void doAttribute(Node poNode) throws Exception {
		System.out.println(getLeadingSpace(iLevel)+"Attribute: "+poNode.getNodeName()+
			" = "+poNode.getNodeValue());
	}

	private void doComment(Node poNode) throws Exception {
		System.out.println(getLeadingSpace(iLevel)+"Comment: "+poNode.getNodeValue());
	}

	private void doProcessingInstruction(Node poNode) throws Exception {
		System.out.println(getLeadingSpace(iLevel)+"PI: "+poNode.getNodeName()+
			" "+poNode.getNodeValue());
	}

	private String getLeadingSpace(int piLevel){
		StringBuffer oBuffer = new StringBuffer(20);
		for (int i=0; i<piLevel; i++){
			oBuffer.append("  ");
		}
		return oBuffer.toString();
	}
}

