I want to unmarhal XML file into array of elements.
Example :
<root>
<animal>
<name>barack</name>
</animal>
<animal>
<name>mitt</name>
</animal>
</root>
I would like an array of Animal elements.
When I try
JAXBContext jaxb = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxb.createUnmarshaller();
Root r = (Root)jaxbUnmarshaller.unmarshal(is);
system.out.println(r.getAnimal.getName());
this display mitt, the last Animal.
I would like to do this :
Animal[] a = ....
// OR
ArrayList<Animal> = ...;
How can I do please ?
You could do the following:
Root
This example would work the same if the field was changed to
List<Animal>orArrayList<Animal>.Animal
Demo
input.xml/Output
For More Information