简体   繁体   中英

JAXB Unmarshall XML with same tag name but different structure

I'm trying to unmarshall this kind of 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>

I have created this kind of class structure:

public class Result {

    private BigDecimal avance;

    private Integer operationStatus;

    private String serviceDesc;

    private Service service;

    @XmlElement(name = "service")
    public String getServiceDesc() {
        return serviceDesc;
    }

    public void setServiceDesc(String serviceDesc) {
        this.serviceDesc = serviceDesc;
    }

    @XmlElement(name = "service")
    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    @XmlElement(name = "avance")
    public BigDecimal getAvance() {
        return avance;
    }

    public void setAvance(BigDecimal avance) {
        this.avance = avance;
    }

    @XmlElement(name = "operation_status")
    public Integer getOperationStatus() {
        return operationStatus;
    }

    public void setOperationStatus(Integer operationStatus) {
        this.operationStatus = operationStatus;
    }
}

and Service looks like this:

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

When I receive response from some external service I'm able to unmarshal that string and result has correct Service class set but serviceDesc is always null . Is there any way how I can unmarshall this structure correctly?


In another question (for which this question is marked as duplicate) case is when you have same tag name but different number of attributes in my case content of the tag is different, also in that question classes to which content should be unmarshaled are child and parent in my case one is String another one some custom object. Think that's why I wasn't able to implement XmlAdapter properly.

Your XML structure is not valid.

You shouldn't have the same tag twice for different structure in the same namespace.

Solutions :

  • Rename your tag ( serviceDesc for example)
  • Use 2 different namespaces ( ns1:service and ns2:service for example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM