我想使用简单的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属性。
谢谢!
编辑:根据建议,我尝试向ShippingInfo添加一个自定义ShippingServiceCost类,如下所示:
@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字段时,我在每个实例中都得到null (即使我知道有数据)。我们非常感谢您的任何建议。
发布于 2019-03-26 12:04:45
对于上面的代码,SimpleXML希望currencyId以<shippingInfo currencyId="USD">
的形式出现。
因此,要解决这个问题,您需要创建另一个名为ShippingServiceCost的类,该类将包含currencyId属性和BigDecimal。这也将解决您的第二个查询。可以通过创建包含currencyId属性的两个类CurrentPrice
和ConvertedCurrentPrice
来实现。
发布于 2020-10-17 22:35:49
唯一可行的解决方案是创建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()));
}
}
注意'currencyId‘是如何使用节点的getAttribute方法设置的。
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());
https://stackoverflow.com/questions/55349422
复制相似问题