import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;


public class DOMUtils {

	public static Document getDocument(String xmlFile) {
		Document doc = null;
		try {
			DocumentBuilderFactory factory =
		 		DocumentBuilderFactory.newInstance();
			//obtain parser:
			DocumentBuilder builder =
		    	factory.newDocumentBuilder();
			// parse an xml document into a DOM tree:
			doc = builder.parse(new File(xmlFile));

		} catch(Exception e) {
			System.out.println(e);
		}
		return doc;
	}


	public static Element getRoot(String xmlFile) {
		return (Element) getDocument(xmlFile).getDocumentElement();
	}

    // access unknown attributes:
	public static void displayAttributes(Element node) {
		NamedNodeMap aNodes = node.getAttributes();
		for(int i = 0; i < aNodes.getLength(); i++) {
			Attr attribute = (Attr)aNodes.item(i);
			System.out.println(attribute.getName() + " = " + attribute.getValue());
		}
	}

	public static void displayContent(Element node) {
		NodeList children = node.getChildNodes();
		for(int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			displayNode(child);
			System.out.println();
		}
	}

	public static void displayNode(Node node) {
		System.out.println("name = " + node.getNodeName());
		System.out.println("value = " + node.getNodeValue());
		System.out.println("type code = " + node.getNodeType());
	}

}

