0

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.

GROX13
  • 4,605
  • 4
  • 27
  • 41
  • Possible duplicate of [JAXB @XmlElements, different types but same name?](http://stackoverflow.com/questions/5007516/jaxb-xmlelements-different-types-but-same-name) – sunil Apr 12 '17 at 09:59

1 Answers1

0

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)
Mickael
  • 4,458
  • 2
  • 28
  • 40
  • That's not my structure I'm implementing integration with external service which returns that kind of response. And I don't see why that xml isn't valid. You can check validity in any xml validator. – GROX13 Apr 12 '17 at 10:05
  • You should try to produce the XSD and you will see this is not valid. – Mickael Apr 12 '17 at 10:12