Here you can find the source of printNode(NodeList nodeList, int level)
private static void printNode(NodeList nodeList, int level)
//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++;/*from w w w . j a va 2 s . com*/ 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; } } } } }