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 7865385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:04:18+00:00 2026-06-03T00:04:18+00:00

im trying to get all element of an XML file and put it into

  • 0

im trying to get all element of an XML file and put it into a ArrayList>> with a recursive method, but i get an error : Exception in thread “main” java.lang.StackOverflowError at java.util.ArrayList.
the error is getting when i make a recursive call : GetAllXml(ListTree);

and i want to get a strcuture like this [[[un]] , [[deux,trois,quatre]] , [[cinq,six,sept],[huit,noeuf],[dix,onze]]]
here is my code:

import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;



public class esperant {

/**
 * @param args
 */


private static List<Element> getChildren(Node parent) 
{
    NodeList nl = parent.getChildNodes();
    List<Element> children = new ArrayList<Element>(nl.getLength());
    for (int i = 0; i < nl.getLength(); i++) {
       Node n = nl.item(i);
       if (n instanceof Element)
            children.add((Element) n);
    }
    return children;
}


public static void GetAllXml(ArrayList<ArrayList<ArrayList<Element>>> ListTree)
{
    ArrayList<ArrayList<Element>> child = new ArrayList<ArrayList<Element>>();

    int level = ListTree.size()-1;

    for (int i=0;i<ListTree.get(level).size();i++)
    {

         for (int j=0;j<ListTree.get(level).get(i).size();j++)
            {
             ArrayList<Element> childOfChild = new ArrayList<Element>();
             childOfChild.addAll(getChildren(ListTree.get(level).get(i).get(j)));
             child.add(childOfChild);   
            }


    }
    ListTree.add(child);
    GetAllXml(ListTree);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList<ArrayList<ArrayList<Element>>> ListTree = new ArrayList<ArrayList<ArrayList<Element>>>();
    ArrayList<ArrayList<Element>> child = new ArrayList<ArrayList<Element>>();
    ArrayList<Element> childOfChild = new ArrayList<Element>();
    try{
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder parser = factory.newDocumentBuilder();
         Document doc = parser.parse("test.xml");
         Element root = doc.getDocumentElement();


         childOfChild.add(root);
         child.add(childOfChild);
         ListTree.add(child);


         GetAllXml(ListTree);




         System.out.println(ListTree);



    }
    catch (Exception e)
    {
         e.printStackTrace();
    }


}

}

here is the xml file :

<?xml version="1.0"  encoding="iso-8859-1"?>
  <un>
     <deux> <cinq></cinq> <six></six> <sept></sept> </deux>
     <trois> <huit></huit><noeuf></noeuf>  </trois>
     <quatre><dix></dix><onze></onze> </quatre>
  </un>
  • 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-03T00:04:19+00:00Added an answer on June 3, 2026 at 12:04 am

    Change Your GetAllXml(X) like this, it would work. As Every one above said , there is no way out from this method.

        final ArrayList<ArrayList<Element>> child = new ArrayList<ArrayList<Element>>();
    
        final int level = ListTree.size() - 1;
    
        for (int i = 0; i < ListTree.get(level).size(); i++)
        {
    
            for (int j = 0; j < ListTree.get(level).get(i).size(); j++)
            {
                final ArrayList<Element> childOfChild = new ArrayList<Element>();
                childOfChild.addAll(getChildren(ListTree.get(level).get(i).get(j)));
                if (childOfChild.size() > 0)
                {
                    child.add(childOfChild);
                }
    
            }
        }
        if (child.size() > 0)
        {
            ListTree.add(child);
            GetAllXml(ListTree);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to validate my XML file with an external DTD. But I get
I'm trying to use the getElementsByTagName(a) method to get all the elements under a
I am trying to get all the rows that exist in allData but not
I am trying to Deserialize an XML file into a few class objects: Artist,
I'm trying to get data from a webpage that serves a XML file periodically
I've got a xml file that looks like this and I'm trying to get
I am trying to extract information from an XML file into an object using
I'm trying to get an array of all the attributes of an element in
I am trying to query an XML file. Below query returns the first element
I'm trying to create an XML output file to import into another program. The

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.