Java tutorial
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { private static int depthOfXML = 1; private static void printNode(NodeList nodeList, int level) { level++; if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { System.out.println(node.getNodeName() + "[" + level + "]"); printNode(node.getChildNodes(), level); // how depth is it? if (level > depthOfXML) depthOfXML = level; } } } } }