How to parse nested xml tags with the same tag name

Viewed 166

I have a not specified number of nested categories, which contain items:

<categories>
    <category>abc
        <category>cde
            <item>someid</item>
            <item>someid</item>
            <item>someid</item>
            <item>someid</item>
        </category>
    </category>
<category>xyz
   <category>zwd
       <category>hgw
           <item>someid</item>
...

The result should be a list of items which are in the most deeply nested category (cde or hgw). Tricky is that there can be more than two level of nesting of categories and I want to save each parent category for the child category.

I already did some xml parsing with Jackson XmlMapper and ObjectMapper, but this use case seems out of reach. So I tried it with javax xml parser but gave up, because the code looks horrible and is hardly readable.

Any idea how to solve this in a more elegant way?

1 Answers

If the task is to quickly pull some values from the xml, then I would use jsoup. Jsoup is actually an html parser, but is also able to parse xml. I'm not sure if jsoup can also validate xml schema and handle namespaces and and ... which is possible with other parsers. But to read a few values jsoup is usually enough for me. If you want to take a look at the Jsoup cookbook or the selector syntax

Maven:

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

Using jsoup your code could look something like:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

public class Example {


    public static void main(String[] args) {
        String xml = "<categories>\n"
                + "    <category>abc\n"
                + "        <category>cde\n"
                + "            <item>someid_1</item>\n"
                + "            <item>someid_2</item>\n"
                + "            <item>someid_3</item>\n"
                + "            <item>someid_4</item>\n"
                + "        </category>\n"
                + "    </category>\n"
                + "    <category>xyz\n"
                + "       <category>zwd\n"
                + "          <category>hgw\n"
                + "             <item>someid_5</item>\n"
                + "          </category>\n"
                + "       </category>\n"
                + "    </category>\n"
                + " </categories>";

        Document doc = Jsoup.parse(xml, "", Parser.xmlParser());

        //if you are interested in Items only
        Elements items = doc.select("category > item");
        items.forEach(i -> {
            System.out.println("Parent text: " +i.parent().ownText());
            System.out.println("Item text: "+ i.text());
            System.out.println();
        });


        //if you are interested in categories having at least one direct item element
        Elements categories = doc.select("category:has(> item)");
        categories.forEach(c -> {
            System.out.println(c.ownText());
            Elements children = c.children();
            children.forEach(ch -> {
                System.out.println(ch.text());
            });
            System.out.println();
        });
    }

}

Output:

Parent text: cde
Item text: someid_1

Parent text: cde
Item text: someid_2

Parent text: cde
Item text: someid_3

Parent text: cde
Item text: someid_4

Parent text: hgw
Item text: someid_5

cde
someid_1
someid_2
someid_3
someid_4

hgw
someid_5
Related