phatperson
phatperson

Reputation: 15

Making java class for soap xml string

How do I make this xml string to a java object? I tried all sorts of things, maybe its the format of the xml?

I am using java 11 and I got a SOAP response by sending a post request. I want to be able to use the outXML string. I did have other classes for Envelope, Body, TransmitResponse, outXML. But I would like to able to call at least the root first.

The error:

javax.xml.bind.UnmarshalException: unexpected element 
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). 
Expected elements are <{http://schemas.xmlsoap.org/soap/envelope/}SOAP-ENV:Envelope>

The xml file

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body SOAP-ENC:encodingStyle="http://schemas.xmlsoap.org/soap/envelope/">
        <NS1:TransmitResponse xmlns:NS1="urn:TransmitterIntf-ITransmitter">
            <return xsi:type="xsd:int">0</return>
            <outXML xsi:type="xsd:string">message</outXML>
        </NS1:TransmitResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The Envelope class:

package com.example

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "", propOrder = { "body" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "SOAP-ENV:Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
public class Envelope {
    
    @XmlElement(name = "SOAP-ENV:Body")
    String body;

    // Getter Methods

    public String getBody() {
        return body;
    }

    // Setter Methods

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

}

Implementation:

            JAXBContext jaxbContext = JAXBContext.newInstance( Envelope.class );
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            StringBuffer xmlStr = new StringBuffer( response.getBody() );
            
            Envelope itransmit = (Envelope) jaxbUnmarshaller.unmarshal( new StreamSource( new StringReader(xmlStr.toString())));
            
            System.out.println(itransmit.getBody());
            return response.toString();

Upvotes: 1

Views: 5550

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 9492

The easiest way to generate your class is to use a wsdl to java plugin. For example if you are using CXF and maven you can use https://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html. This will generate the java code for you.

Also you can try, this is untested code:

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

    // Getter Methods

    public String getBody() {
        return body;
    }

    // Setter Methods

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

}

Upvotes: 1

Related Questions