Unmarshalling multiple XML elements of the same name to a list using JAXB

I am trying to parse an XML message into a Java object. It works for me for the most part, but there is one problem for which I am stuck. I have a circuit that looks like this:

<DeliveryDetails> <Name>Ed</Name> <Location>Toronto</Location> <Event> <Date>2013-05-06</Date> <Time>12:12</Time> <Description>MARKHAM</Description> </Event> <Event> <Date>2013-05-07</Date> <Time>05:12</Time> <Description>MARKHAM</Description> </Event> <Event> <Date>2013-05-08</Date> <Time>15:12</Time> <Description>MARKHAM</Description> </Event> </DeliveryDetails> 

Now the problem is that the JAXB ObjectFactory saves only the last event. If there was an element that wrapped events (), I would know how to handle it using the Wrapper XML element. But since there is no wrapper, I'm not sure what to do. Does anyone have any ideas?

I assume that ObjectFactory receives all events, but constantly rewrites the old with the latest. There must be some way to tell him to save every single event, and not just write the same every time, but I don’t know how to do it.

+4
source share
1 answer

By default, the JAXB implementation (JSR-222) will present the List as multiple items with the same name. As long as you have something like the following, you'll be fine:

 @XmlRootElement(name="DeliveryDetails") @XmlAccessorType(XmlAccessType.FIELD) public class DeliveryDetails { @XmlElement(name="Name") private String name; @XmlElement(name="Location") private String location; @XmlElement(name="Event") private List<Event> events; } 

Additional Information

+6
source

Source: https://habr.com/ru/post/1482652/


All Articles