Laucer
Laucer

Reputation: 31

Jackson parsing XML

I'm trying to serialize POJO class to Amazon XML format to aggregate the date from the service.

The goal is to have an xml like:

<ShipmentEventList>
    <ShipmentEvent>
        <ShipmentItemList>
            <ShipmentItem></ShipmentItem>
        </ShipmentItemList>
        <AmazonOrderId>AAAA</AmazonOrderId>
        <PostedDate>BBBB</PostedDate>
        <MarketplaceName>CCCC</MarketplaceName>
        <SellerOrderId>DDDD</SellerOrderId>
    </ShipmentEvent>
</ShipmentEventList>

Here are my POJO classes

ShipmentEventList

public class ShipmentEventList {

   @JacksonXmlElementWrapper(localName = "ShipmentEventList")
   @JacksonXmlProperty(localName = "ShipmentEvent")
   private List<ShipmentEvent> shipmentEventList;
}

ShipmentEvent

@JacksonXmlRootElement(localName = "ShipmentEvent")
public class ShipmentEvent {

    @JacksonXmlElementWrapper(localName = "ShipmentItemList")
    private List<ShipmentItem> shipmentItemList;

    @JacksonXmlProperty(localName = "AmazonOrderId")
    private String amazonOrderId;

    @JacksonXmlProperty(localName = "PostedDate")
    private String postedDate;

    @JacksonXmlProperty(localName = "MarketplaceName")
    private String marketplaceName;

    @JacksonXmlProperty(localName = "SellerOrderId")
    private String sellerOrderId;

}

Unfortunatelly, as a result of the serialization I have:

<ShipmentEventList>
    <ShipmentEventList>
        <ShipmentEvent>
            <AmazonOrderId>A</AmazonOrderId>
            <PostedDate>B</PostedDate>
            <MarketplaceName>C</MarketplaceName>
            <SellerOrderId>D</SellerOrderId>
        </ShipmentEvent>
        <ShipmentEvent>
            <AmazonOrderId>B</AmazonOrderId>
            <PostedDate>C</PostedDate>
            <MarketplaceName>D</MarketplaceName>
            <SellerOrderId>E</SellerOrderId>
        </ShipmentEvent>
    </ShipmentEventList>
</ShipmentEventList>

Could you explain me how does the serialization of collections work in Jackson?

Upvotes: 0

Views: 51

Answers (1)

Michał Ziober
Michał Ziober

Reputation: 38625

You need to set useWrapping flag to false:

class ShipmentEventList {

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "ShipmentEvent")
    private List<ShipmentEvent> shipmentEventList;
}

Upvotes: 1

Related Questions