简体   繁体   中英

Parsing an XML file with JDOM, error StackOverflowError

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>

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);
    }

Every call on GetAllXml(X), whatever else it does, ends up calling GetAllXml(X) passing the same value as its argument. So it's obvious that it will recurse infinitely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM