Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB解组具有相同标记名但结构不同的XML_Java_Jaxb_Unmarshalling - Fatal编程技术网

Java JAXB解组具有相同标记名但结构不同的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>

我正在尝试解组这种XML:

<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

可能的重复不是我的结构,我正在实现与外部服务的集成,该服务返回这种响应。我不明白为什么xml是无效的。您可以在任何xml验证器中检查有效性。您应该尝试生成XSD,您将看到这是无效的。
@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;
    }
}