简单的xml为属性字段返回空值

时间:2019-03-26 03:26:10

标签: java xml simple-framework

我想使用简单XML将以下XML反序列化为POJO:

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

我已经创建了以下课程。但是,我无法正确地对currencyId属性进行反序列化。

@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;
    }
}

在反序列化后,我希望currencyId的值为“ USD”,但是我得到的是空值。所有元素值似乎都已正确反序列化。有人对如何解决这个问题有建议吗?

此外,在以下情况下:

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

在两个不同的元素上有两个名为currencyId的属性,我该如何将它们反序列化为单独的字段?我创建了一个类似的SellingStatus类,但是不确定如何区分currencyId属性。

谢谢!

编辑:根据建议,我尝试将自定义ShippingServiceCost类添加到ShippingInfo,如下所示:

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

反过来看起来像这样:

public class ShippingServiceCost {

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

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

    // getters and setters
}

但是当我尝试同时访问shippingServiceCost字段和currencyId字段时,在每个实例中我都为空(即使我知道有数据)。任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

对于上述代码,SimpleXML希望currencyId以<shippingInfo currencyId="USD">的形式出现。

因此,要解决此问题,您需要创建另一个名为ShippingServiceCost的类,该类将包含currencyId属性和BigDecimal 这也将解决您的第二个查询。您可以通过创建两个类CurrentPriceConvertedCurrentPrice来做到这一点,这两个类将包含currencyId属性。

答案 1 :(得分:0)

唯一可行的解​​决方案是创建Converter类,请参见下面的代码:

public class ShippingInfoConverter implements Converter<ShippingInfo> {

@Override
public ShippingInfo read(InputNode inputNode) throws Exception {
    ShippingInfo shippingInfo = new ShippingInfo();
    InputNode shippingServiceCostNode = inputNode.getNext("shippingServiceCost");
    shippingInfo.setShippingServiceCost(new BigDecimal(shippingServiceCostNode.getValue()));
    shippingInfo.setCurrencyId(shippingServiceCostNode.getAttribute("currencyId").getValue());
    shippingInfo.setShippingType(inputNode.getNext("shippingType").getValue());
    shippingInfo.setShipToLocations(inputNode.getNext("shipToLocations").getValue());
    shippingInfo.setExpeditedShipping(Boolean.parseBoolean(inputNode.getNext("expeditedShipping").getValue()));
    shippingInfo.setOneDayShippingAvailable(Boolean.parseBoolean(inputNode.getNext("oneDayShippingAvailable").getValue()));
    shippingInfo.setHandlingTime(Integer.valueOf(inputNode.getNext("handlingTime").getValue()));
    return shippingInfo;
}

@Override
public void write(OutputNode outputNode, ShippingInfo shippingInfo) throws Exception {
    OutputNode shippingServiceCostNode = outputNode.getChild("shippingServiceCost");
    shippingServiceCostNode.setValue(shippingInfo.getShippingServiceCost().toString());
    shippingServiceCostNode.setAttribute("currencyId", shippingInfo.getCurrencyId());
    outputNode.getChild("shippingType").setValue(shippingInfo.getShippingType());
    outputNode.getChild("shipToLocations").setValue(shippingInfo.getShipToLocations());
    outputNode.getChild("expeditedShipping").setValue(Boolean.toString(shippingInfo.isExpeditedShipping()));
    outputNode.getChild("oneDayShippingAvailable").setValue(Boolean.toString(shippingInfo.isOneDayShippingAvailable()));
    outputNode.getChild("handlingTime").setValue(Integer.toString(shippingInfo.getHandlingTime()));
}

}

请注意如何使用节点的getAttribute方法设置“ currencyId”。

shippingInfo.setCurrencyId(shippingServiceCostNode.getAttribute("currencyId").getValue());

还要注意元素“ shippingServiceCost”如何获取属性

shippingServiceCostNode.setAttribute("currencyId", shippingInfo.getCurrencyId());

要使此功能正常运行,还需要一些其他事情,从您的POJO开始

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

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

    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;
    }

}

在下面的行中添加将SimpleXML指向转换器类

@Convert(ShippingInfoConverter.class)

另一个更改是删除@Attribute批注。 最后一件事是您的驱动程序类需要启用AnnotationStrategy 序列化和反序列化对象时。

Serializer serializer = new Persister(new AnnotationStrategy());