I am looking for a simple way to parse an XML structure with a repeated element using Jackson. Here is a simplified example:
<root>
<a>
<x>
<b>some content</b>
<c>some content</c>
</x>
<x>
<b>some content</b>
<c>some content</c>
</x>
...
<x>
<b>some content</b>
<c>some content</c>
</x>
</a>
... some other content ...
</root>
I would like to collect all the x
elements in a list or array The problem is that when using something like:
XmlMapper().readTree(xml).get("a").fields()
the result contains only the last instance of x
so it looks like some map key gets overwritten. There is a solution using JsonParser
e.g.: XmlMapper().createParser(xml)
but it's a bit icky.
Is there a better way?
Edit
The way I read the "Known Limitations" section in the README , XML seems to be a second class citizen in Jackson. I accept @galuszkak 's answer as it is what it is , but to my mind when a developer invokes XmlMapper().readTree(xml)
they do not expect XML processing to be shoehorned into JSON processing model with the limitations that come with that approach.