I have a simple XML file that has the following structure
<?xml version = "1.0" encoding = "UTF-8"?>
<EXPORT id="0" >
<PUB id="1">
<PGR id="2" name="siblingparent" >
<PGR id="3" name="sibling1"/>
<PGR id="4" name="sibling2"/>
</PGR>
</PUB>
</EXPORT>
I have this test to see if Jackson's XmlMapper
using the readTree()
method will parse the file as JsonNode
correctly.
@Test
public void test() throws IOException {
JsonNode node = parser.getNodeFromXML(siblingXML.getFile());
assertThat(node.findValues("PGR")).size().isEqualTo(3);
}
The test fails because only 1
PGR
element has been found.
This is the logic for parser.getNodeFromXML
:
public JsonNode getNodeFromXML(File xmlFile) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
return xmlMapper.readTree(xmlFile);
}
And this is how the JsonNode
Object / the variable node
in the test is returned:
{
"id": "0",
"PUB": {
"id": "1",
"PGR": {
"id": "2",
"name": "siblingparent",
"PGR": {
"id": "4",
"name": "sibling2"
}
}
}
}
As you can see only sibling2
to has been parsed by the XmlMapper
. I already looked for flags to allow siblings or whatever. Was not able to find a solution yet. May I misunderstand what a Tree is here? Does it ignore siblings on purpose?