0

I have to parse an XML file using JDOM and get some infos from all his elements.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element1>something</element1>
    <element2>
        <subelement21>moo</subelement21>
        <subelement22>
            <subelement221>toto</subelement221>
            <subelement222>tata</subelement222>
        </subelement22>
    </element2>
</root> 

So, for the element1 it's easy. But for the element2 I have to go through his children and if the children has children go through them too and so on.

public static void getInfos(Vector<String> files) {     
    Document document = null;
    Element root = null;

    SAXBuilder sxb = new SAXBuilder();

    for (int i =0 ; i< files.size() ; i++)
    {
        System.out.println("n°" + i + " : " + files.elementAt(i));
        try
        {
            document = sxb.build(files.elementAt(i));
            root = document.getRootElement();

            List<?> listElements = root.getChildren();
            Iterator<?> it = listElements.iterator();

            while(it.hasNext())
            {
                Element courant = (Element)it.next();
                System.out.println(courant.getName());

                if(courant.getChildren().size() > 0)
                {
                    // here is the problem -> the element has a children
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }   
    }
}

What do you suggest in this case, like a recursive call or something else so I can use the same function.

Thanks.

Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174

2 Answers2

1

I would use SAX. I'd keep a stack in the contenthandler that tracked what my current path was in the document, and keep a buffer that my characters method appended to. In endElement I'd get the content from the buffer and clear it out, then use the current path to decide what to do with it.

(this is assuming this document has no mixed-content.)

Here's a link to an article on using SAX to process complex XML documents, it expands on what I briefly described into an approach that handles recursive data structures. (It also has a predecessor article that is an introduction to SAX.)

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

You could consider using XPath to get the exact elements you want. The example here uses namespaces but the basic idea holds.

Community
  • 1
  • 1
laz
  • 28,320
  • 5
  • 53
  • 50