Java JAXB解组具有相同标记名但结构不同的XML
我正在尝试解组这种XML:Java JAXB解组具有相同标记名但结构不同的XML,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,我正在尝试解组这种XML: <result> <avance>0.0000</avance> <operation_status>0</operation_status> <service>Bank Account</service> <service> <min_amount>1.00</min_amount>
<result>
<avance>0.0000</avance>
<operation_status>0</operation_status>
<service>Bank Account</service>
<service>
<min_amount>1.00</min_amount>
<max_amount>1499.00</max_amount>
<currency>USD</currency>
</service>
</result>
而服务
如下所示:
@XmlRootElement
public class Service {
private BigDecimal minAmount;
private BigDecimal maxAmount;
private String currency;
@XmlElement(name = "min_amount")
public BigDecimal getMinAmount() {
return minAmount;
}
public void setMinAmount(BigDecimal minAmount) {
this.minAmount = minAmount;
}
@XmlElement(name = "max_amount")
public BigDecimal getMaxAmount() {
return maxAmount;
}
public void setMaxAmount(BigDecimal maxAmount) {
this.maxAmount = maxAmount;
}
@XmlElement(name = "currency")
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
}
当我收到来自某个外部服务的响应时,我能够解组该字符串,并且结果具有正确的service
类集,但是serviceDesc
总是null
。有什么方法可以正确地解开这个结构吗
在另一个问题(此问题标记为重复)中,案例是当您具有相同的标记名,但在我的案例中属性的数量不同时,标记的内容也不同,同样在该问题中,应该取消标记内容的类是子类和父类。在我的案例中,一个是
String
另一个是某个自定义对象。我想这就是为什么我不能正确地实现XmlAdapter
您的XML结构无效。
对于同一命名空间中的不同结构,不应该有两次相同的标记
解决方案:
- 重命名您的标记(例如,
)serviceDesc
- 使用两个不同的名称空间(
和ns1:service
)ns2:service
@XmlRootElement
public class Service {
private BigDecimal minAmount;
private BigDecimal maxAmount;
private String currency;
@XmlElement(name = "min_amount")
public BigDecimal getMinAmount() {
return minAmount;
}
public void setMinAmount(BigDecimal minAmount) {
this.minAmount = minAmount;
}
@XmlElement(name = "max_amount")
public BigDecimal getMaxAmount() {
return maxAmount;
}
public void setMaxAmount(BigDecimal maxAmount) {
this.maxAmount = maxAmount;
}
@XmlElement(name = "currency")
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
}