杰克逊解析XML

huangapple go评论73阅读模式
英文:

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?

英文:

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?

答案1

得分: 1

你需要将 useWrapping 标志设置为 false

class ShipmentEventList {

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

You need to set useWrapping flag to false:

class ShipmentEventList {

	@JacksonXmlElementWrapper(useWrapping = false)
	@JacksonXmlProperty(localName = &quot;ShipmentEvent&quot;)
	private List&lt;ShipmentEvent&gt; shipmentEventList;
}

huangapple
  • 本文由 发表于 2020年9月22日 21:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64010880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定