Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7844595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:55:45+00:00 2026-06-02T16:55:45+00:00

i want to get the maximum depth of an XML file using a recursive

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:55:49+00:00Added an answer on June 2, 2026 at 4:55 pm

    You can do it as a one-liner using XPath 2.0:

    max(for $n in //* return count($n/ancestor::*))
    

    Even in Java, you’re making it much more difficult than it is:

    public int maxDepth(Node node) {
      int max = 0;
      NodeList kids = node.getChildNodes();
      if (kids.getLength() == 0) {
         return 0;
      }
      for (int i=0; i<kids.getLength(); i++) {
         int kidMax = maxDepth(kids.item(i);
         if (kidMax > max) max = kidMax;
      }
      return max + 1;
    }
    

    Not tested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the maximum count I have to execute a loop for
I have a filesystem like XML structure, and now I want to get the
So I have an NSFont, and I want to get the maximum dimensions for
How do I get the maximum of a formula using smt-lib2? I want something
I have an xml string like this and I want to get attribute value
I want to get the maximum values from two images, while preserving other information.
I'm building a one-page(one file) site and want to get insight as to whether
I want to get the maximum record based on a field, but the Max()
Ive got a Zend_Db_Table Now I want to get the maximum available value of
I have folowing sql query an di want to get previous of max value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.