递归解析XML文件jdom

时间:2022-03-29 12:33:17

I have a trouble with parsing an XML File called "test.xml" like this :

我在解析名为“test.xml”的XML文件时遇到了麻烦,如下所示:

<?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>

I want to get a structure like this : [[[un]] , [[deux,trois,quatre]] , [[cinq,six,sept],[huit,noeuf],[dix,onze]]] But i get this [[[cinq, six, sept]], [[huit, noeuf]], [[dix, onze]], [[deux, trois, quatre]]]

我想得到这样的结构:[[[un]],[[deux,trois,quatre]],[[cinq,six,sept],[huit,noeuf],[dix,onze]]但是我得到这[[[[cinq,six,sept]],[[huit,noeuf]],[[dix,onze]],[[deux,trois,quatre]]]

here is my code:

这是我的代码:

 import java.util.ArrayList;

 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;

 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Element;

public class Wassim {

/**
 * @param args
 */

public static void GetAllXml(ArrayList<ArrayList<ArrayList<String>>> ListTree, Node node)
{
    ArrayList<ArrayList<String>> child = new ArrayList<ArrayList<String>>(); 
    ArrayList<String> childOfChild = new ArrayList<String>();

    NodeList nl= node.getChildNodes();
    if (nl.getLength()>0)
    {
    for (int i=0;i<nl.getLength();i++)
    {

          Node n = nl.item(i);
          if (n instanceof Element)
          {

              childOfChild.add(n.getNodeName());
              GetAllXml(ListTree, n);
          }
    }
    child.add(childOfChild);
    ListTree.add(child);
    }

}


public static void main(String[] args) {
    // TODO Auto-generated method stub

     try{
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder parser = factory.newDocumentBuilder();
         Document doc = parser.parse("test.xml");
         Node root = doc.getDocumentElement();
         ArrayList<ArrayList<ArrayList<String>>> ListTree = new ArrayList<ArrayList<ArrayList<String>>>();
         GetAllXml( ListTree, root);
         System.out.println(ListTree);

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

}
}

1 个解决方案

#1


1  

It is a two step procedure for each element:

每个元素都有两个步骤:

  1. Add all child elements to the list
  2. 将所有子元素添加到列表中
  3. Process all child elements
  4. 处理所有子元素

Something like this:

像这样的东西:

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<String>>> ListTree, Node node) {
    ArrayList<ArrayList<String>> child = new ArrayList<ArrayList<String>>();
    ArrayList<String> childOfChild = new ArrayList<String>();

    List<Element> children = getChildren(node);
    // add children node names
    for (Element e : children)
         childOfChild.add(e.getNodeName());

    if (childOfChild.size() > 0) {
        child.add(childOfChild);
        ListTree.add(child);
    }

    // process next level
    for (Element e : children)
        GetAllXml(ListTree, e);
}

#1


1  

It is a two step procedure for each element:

每个元素都有两个步骤:

  1. Add all child elements to the list
  2. 将所有子元素添加到列表中
  3. Process all child elements
  4. 处理所有子元素

Something like this:

像这样的东西:

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<String>>> ListTree, Node node) {
    ArrayList<ArrayList<String>> child = new ArrayList<ArrayList<String>>();
    ArrayList<String> childOfChild = new ArrayList<String>();

    List<Element> children = getChildren(node);
    // add children node names
    for (Element e : children)
         childOfChild.add(e.getNodeName());

    if (childOfChild.size() > 0) {
        child.add(childOfChild);
        ListTree.add(child);
    }

    // process next level
    for (Element e : children)
        GetAllXml(ListTree, e);
}