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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:43:28+00:00 2026-06-02T10:43:28+00:00

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

  • 0

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

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

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 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-02T10:43:31+00:00Added an answer on June 2, 2026 at 10:43 am

    It is a two step procedure for each element:

    1. Add all child elements to the list
    2. Process all child elements

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<?xml version=1.0 encoding=ISO-8859-1?> <users> <user number=0775547857> <step stepnumber=1>complete</step> <step stepnumber=2>complete</step> <step stepnumber=3>complete</step> </user> <user
My problem is specifically about some trouble I'm having parsing an Inkscape (XML) file,
I am parsing a XML file with a format of: **NOTE: This is a
I have some trouble understanding parsing XML structures with SAX. Let's say there is
This is the XML I'm trying to parse. I'm having trouble parsing the entry
I have trouble parsing a HTML table using Nokogiri and Ruby. My HTML table
I have trouble finding way to correctly refactor this code so that there would
I'd like to read in an XML file that is stored on my Tomcat
I'm having trouble parsing the following link. I'd like to be able to extract
I'm having some trouble using XmlReader to read an XML file. I can open

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.