Home > database >  Unmarshalling retruns duplicate empty values
Unmarshalling retruns duplicate empty values

Time:11-16

I've tried to unmarshal this xml snippet

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
        <ns1:tMLHeader soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0"
                       xmlns:ns1="http://tml.t1m1.org/tML.Transport.xsd">
            <ns1:TransportID>111-23-Some_code--12</ns1:TransportID>
            <ns1:ApplicationType>Any_type</ns1:ApplicationType>
            <ns1:ApplicationVersion>1.0</ns1:ApplicationVersion>
            <ns1:From>XXX</ns1:From>
            <ns1:To>YY_G</ns1:To>
            <ns1:SendTimestamp>2021-11-11T10:25:56.858-06:00</ns1:SendTimestamp>
            <ns1:RetryCount>0</ns1:RetryCount>
        </ns1:tMLHeader>
    </soapenv:Header>
    <soapenv:Body>
        <processSyncRequestResponse xmlns="java:lsr.webservice.wisor.com">
            <response xmlns="">mocked response</response>
        </processSyncRequestResponse>

    </soapenv:Body>
</soapenv:Envelope>

But despite on my actions, the result is always almost the same (either 4, or 2 empty fields):

{
    "Header": null,
    "Body": null,
    "header": null,
    "body": null
}

My previous actions were: define constructor (all fields, no fields) and add setters and getters and mark as @XmlElement. Use @XmlType for subclasses with property order. Also some attempts with package-info and ObjectFactory. The current version I've tried from Stack Overflow's example: since all the fields' names are matching xml fields, use only @XmlAccessorType(XmlAccessType.FIELD) along with getters and setters. But still the same result. Could someone please help me and explain how it works?

Unmarshalling:

File xml = new File("test.xml");
return (Envelope)JAXBContext
                    .newInstance(Envelope.class)
                    .createUnmarshaller()
                    .unmarshal(xml);

Classes:

@XmlRootElement(name="Envelope",namespace = "http://schemas.xmlsoap.org/soap/envelope/")
@XmlAccessorType(XmlAccessType.FIELD)
public class Envelope {
    public Header Header;
    public Body Body;

    public Header getHeader() {
        return Header;
    }

    public void setHeader(Header header) {
        Header = header;
    }

    public Body getBody() {
        return Body;
    }

    public void setBody(Body body) {
        Body = body;
    }
}


@XmlAccessorType(XmlAccessType.FIELD)
public class Header {
    public tMLHeader tMLHeader;

    public tMLHeader gettMLHeader() {
        return tMLHeader;
    }

    public void settMLHeader(tMLHeader tMLHeader) {
        this.tMLHeader = tMLHeader;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
public class tMLHeader {

    public String TransportID;

    public String ApplicationType;

    public double ApplicationVersion;

    public String From;

    public String To;

    public Date SendTimestamp;

    public int RetryCount;

    public String getTransportID() {
        return TransportID;
    }

    public void setTransportID(String transportID) {
        TransportID = transportID;
    }
....//other setters and getters


@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
    @XmlElement(name = "processSyncRequestResponse")
    public ProcessSyncRequestResponse processSyncRequestResponse;

    public ProcessSyncRequestResponse getProcessSyncRequestResponse() {
        return processSyncRequestResponse;
    }

    public void setProcessSyncRequestResponse(ProcessSyncRequestResponse processSyncRequestResponse) {
        this.processSyncRequestResponse = processSyncRequestResponse;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
public class ProcessSyncRequestResponse {

    public String response;

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }
}

Thanks in advance

CodePudding user response:

You need to add namespace for the fields.

For the Envelope class that can look like this.

@XmlRootElement(name="Envelope",namespace = "http://schemas.xmlsoap.org/soap/envelope/")
@XmlAccessorType(XmlAccessType.FIELD)
public class Envelope {
    @XmlElement(name="Header", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    private Header header;

    @XmlElement(name="Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    public Body body;

    public Header getHeader() {
        return header;
    }

    public void setHeader(Header header) {
        this.header = header;
    }

    public Body getBody() {
        return Body;
    }

    public void setBody(Body body) {
        this.body = body;
    }
}

(Add @XmlElement annotation to all the fields in Header and tMLHeader)

  • Related