i want to get the maximum depth of an XML file using a recursive method firstly i have declared the variable
public static int maxdepth=0;
private static void GetDepth(NodeList nl, int level,int maxdepth) {
level++;
if (maxdepth<level)
{
maxdepth= level;
}
if(nl != null && nl.getLength() > 0){
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n instanceof Element)
{
GetDepth(n.getChildNodes(), level, maxdepth);
}
}
}
}
public static void main(String[] args) {
NodeList nl = root.getChildNodes();
GetDepth(nl,level,maxdepth);
System.out.println(maxdepth);
}
and when i would display the value of variable maxdepth, I receive the value 0, as the declaration
You can do it as a one-liner using XPath 2.0:
Even in Java, you’re making it much more difficult than it is:
Not tested.