Я хочу использовать простой 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, я получаю нулевое значение в каждом случае (хотя я знаю, чтоесть данные).Любые предложения приветствуются.