question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

readTree can't parse an xml with a collection of elements with the same tag (UPDATE can in 2.12+)

See original GitHub issue

Hi, this seems such a basic functionality that I must have missed something in the configuration of the XmlMapper.

I am trying to parse something like this:

<foo>
    <bars>
        <bar id="one">bar one</bar>
        <bar id="two">bar two</bar>
        <bar id="three">bar three</bar>
        <bar id="four">bar four</bar>
        <bar id="five">bar five</bar>
    </bars>
</foo>

It seems like only the last ’ bar five’ gets parsed, which indicates that an hashmap has been used to collect the tags rather than an array.

Here’s a simple test to reproduce the problem:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;


public class JacksonXMLTest {

    private String xml =
            "<foo>\n" +
            "\t<bars>\n" +
            "\t\t<bar id=\"one\">bar one</bar>\n" +
            "\t\t<bar id=\"two\">bar two</bar>\n" +
            "\t\t<bar id=\"three\">bar three</bar>\n" +
            "\t\t<bar id=\"four\">bar four</bar>\n" +
            "\t\t<bar id=\"five\">bar five</bar>\n" +
            "\t</bars>\n" +
            "</foo>";

    @Test
    public void itDoesntWork() throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        JsonNode jsonNode = xmlMapper.readTree(xml);

        assertEquals(5, jsonNode.findValues("bar").size());
    }
}

built with

compile('com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.3')

Is there something specific to setup in the JacksonXmlModule to get it working? Or this sort of XML format is just not supported?

Thanks in advance

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:10
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
cawenacommented, Sep 19, 2017
                JSONObject xmlJsonObj = XML.toJSONObject(YourXMLString);
                String jsonPrettyPrintString = xmlJsonObj.toString(PRETTY_PRINT_INDENT_FACTOR);
                JsonNode jsonNodeNew = mapper.readTree(jsonPrettyPrintString);
1reaction
cowtowncodercommented, Jun 21, 2021

Note: Jackson version 2.12 DOES allow reading of duplicate elements with readTree(). (also works for nominal type of java.lang.Object, so-called “untyped” binding).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson 2.12: improved XML module - cowtowncoder - Medium
Jackson 2.12 handles this case in a way that allows ignoring of “unexpected” attributes, while still supporting ability to alternatively map ...
Read more >
Using Jackson XmlMapper and readTree() to parse a XML file ...
I have this test to see if Jackson's XmlMapper using the readTree() method will parse the file as JsonNode correctly.
Read more >
FasterXML/jackson-databind - Gitter
Ideally I would want jackson-module-kotlin to respect collection element types' nullity, however I realize this is idealistic and may be difficult.
Read more >
ObjectMapper (jackson-databind 2.14.1 API) - javadoc.io
Factory method for constructing ObjectReader that will read or update ... Same as readTree(InputStream) except content read from passed-in byte array.
Read more >
A Git client for Windows Version 2.13.0 - TortoiseGit
Branch is just head of commits. Tag is friend name of commit hash. Toolkit design. Following the Unix tradition, Git is a collection...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found