Cloud Stack Ninja

I want to use Simple XML to deserialize the following XML into a POJO:

<shippingInfo>
   <shippingServiceCost currencyId="USD">9.8</shippingServiceCost>
   <shippingType>Flat</shippingType>
   <shipToLocations>Worldwide</shipToLocations>
   <expeditedShipping>true</expeditedShipping>
   <oneDayShippingAvailable>false</oneDayShippingAvailable>
   <handlingTime>3</handlingTime>
</shippingInfo>

I have created the following class to do so. However, I'm having trouble in that the currencyId attribute isn't being properly deserialized.

@Root(name = "shippingInfo")
public class ShippingInfo {

    @Element(name = "shippingServiceCost", required = false)
    private BigDecimal shippingServiceCost;

    @Attribute(name = "currencyId", required = false)
    private String currencyId;

    @Element(name = "shippingType", required = false)
    private String shippingType;

    @Element(name = "shipToLocations" ,required = false)
    private String shipToLocations;

    @Element(name = "expeditedShipping", required = false)
    private Boolean expeditedShipping;

    @Element(name = "oneDayShippingAvailable", required = false)
    private Boolean oneDayShippingAvailable;

    @Element(name = "handlingTime", required = false)
    private Integer handlingTime;

    // Getters & Setters

    public BigDecimal getShippingServiceCost() {
        return shippingServiceCost;
    }

    public void setShippingServiceCost(BigDecimal shippingServiceCost) {
        this.shippingServiceCost = shippingServiceCost;
    }

    public String getCurrencyId() {
        return currencyId;
    }

    public void setCurrencyId(String currencyId) {
        this.currencyId = currencyId;
    }

    public String getShippingType() {
        return shippingType;
    }

    public void setShippingType(String shippingType) {
        this.shippingType = shippingType;
    }

    public String getShipToLocations() {
        return shipToLocations;
    }

    public void setShipToLocations(String shipToLocations) {
        this.shipToLocations = shipToLocations;
    }

    public Boolean isExpeditedShipping() {
        return expeditedShipping;
    }

    public void setExpeditedShipping(Boolean bool) {
        this.expeditedShipping = bool;
    }

    public Boolean isOneDayShippingAvailable() {
        return oneDayShippingAvailable;
    }

    public void setOneDayShippingAvailable(Boolean bool) {
        this.oneDayShippingAvailable = bool;
    }

    public Integer getHandlingTime() {
        return handlingTime;
    }

    public void setHandlingTime(Integer days) {
        this.handlingTime = days;
    }
}

I would expect the value of currencyId to be "USD" after deserializing, but I'm getting null. All the element values appear to deserialize properly. Does anyone have a suggestion on how to fix this?

Moreover, in a case such as the following instance:

<sellingStatus>
    <currentPrice currencyId="USD">125.0</currentPrice>
    <convertedCurrentPrice currencyId="USD">125.0</convertedCurrentPrice>
    <bidCount>2</bidCount>
    <sellingState>EndedWithSales</sellingState>
</sellingStatus>

Where there are two attributes named currencyId on two distinct elements, how can I go about deserializing these into separate fields? I have created a similar SellingStatus class but am unsure how to distinguish between the currencyId attributes.

Thank you!

Edit: Per suggestions I tried adding a custom ShippingServiceCost class to ShippingInfo as follows:

@Element(name = "shippingServiceCost", required = false)
    private ShippingServiceCost shippingServiceCost;

Which in turn looks like this:

public class ShippingServiceCost {

    @Element(name = "shippingServiceCost", required = false)
    private BigDecimal shippingServiceCost;

    @Attribute(name = "currencyId", required = false)
    private String currencyId;

    // getters and setters
}

But when I try to access both the shippingServiceCost field and the currencyId field, I get null in every instance (even though I know there is data). Any suggestions are greatly appreciated.



Read more here: https://stackoverflow.com/questions/55349422/simple-xml-returns-null-value-for-attribute-field

Content Attribution

This content was originally published by opnightfall1771 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: