readTree can't parse an xml with a collection of elements with the same tag (UPDATE can in 2.12+)
See original GitHub issueHi, 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:
- Created 8 years ago
- Reactions:10
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note: Jackson version 2.12 DOES allow reading of duplicate elements with
readTree()
. (also works for nominal type ofjava.lang.Object
, so-called “untyped” binding).