如何在 java SimpleXML 中反序列化对象

How to deserialize object in java SimpleXML

我必须通过 soap 发送请求。我使用 Retrofit2。 我无法转换和反序列化响应。在 xml 类型中,例如 "xsi" 实例 "xmlns" 这是我的问题。

这是我的回复

 <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:requestCPResponse xmlns:ns2="http://incomingwebchannel.hrz.jpos.org/">
         <return xsi:type="ns2:jiringBillInqOut" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <refNo>091015900983</refNo>
            <responseCode>03</responseCode>
         </return>
      </ns2:requestCPResponse>
   </S:Body>
</S:Envelope>

这是我改造后的界面

 @POST("/nafis-web-3/services/mobile")
 Call<UsStatesResponseEnvelope> requestCP(@Body UsStatesRequestEnvelope body);

这是我的请求代码

new RequestCallback<>(activity, true, ApiClient.createApi(activity).requestCP(envelope), new RequestListener<UsStatesResponseEnvelope>() {
                @Override
                public void onResponse(@NonNull final Response<UsStatesResponseEnvelope> response) {

                    try {
                        com.teskaco.transactionsample.utils.Log.e(response.body().getBody().getUsStatesCPResponse().getStatesResponseReturn().getResponseCode());



                    } catch (Exception e) {

                        Log.e("TAG", e.getMessage());
                    }
                }

                @Override
                public void onFailure(@NonNull int code, @NonNull JsonObject jsonObject) {
                }
            });

这是我的模特。 UsStatesResponseBody.clss

@Root(name = "S:Envelope")
@NamespaceList({
        @Namespace( prefix = "S", reference = "http://schemas.xmlsoap.org/soap/envelope/")})
public class UsStatesResponseEnvelope {

    @Element(name = "S:Body", required = false)
    private UsStatesResponseBody body;

    public UsStatesResponseBody getBody() {
        return body;
    }

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

class UsStatesResponseBody.clss

    @Root(name = "S:Body", strict = false)
public class UsStatesResponseBody {

    @Element(name = "ns2:requestCPResponse",required = false)
    private UsStatesCPResponse cpResponse;

    public UsStatesCPResponse getUsStatesCPResponse() {
        return cpResponse;
    }

    public void UsUsStatesCPResponse(UsStatesCPResponse responseReturn) {
        this.cpResponse = responseReturn;
    }
}

UsStatesCPResponse.clss

@Root(name = "ns2:requestCPResponse", strict = false)
@NamespaceList({
        @Namespace( prefix = "ns2", reference = "http://incomingwebchannel.hrz.jpos.org/")})
public class UsStatesCPResponse {

    @Element(name = "return",required = false)
    private UsStatesResponseReturn responseReturn;

    public UsStatesResponseReturn getStatesResponseReturn() {
        return responseReturn;
    }

    public void UsStatesResponseReturn(UsStatesResponseReturn responseReturn) {
        this.responseReturn = responseReturn;
    }
}

UsStatesResponseReturn.clss

@Root(name = "return", strict = false)
@NamespaceList({
        @Namespace( prefix = "xsi:type", reference = "ns2:jiringBillInqOut"),
        @Namespace( prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance")
})
public class UsStatesResponseReturn {

    @Element(name = "refNo",required = false)
    private String refNo;

    @Element(name = "responseCode",required = false)
    private String responseCode;

    public String getRefNo() {
        return refNo;
    }

    public void setRefNo(String refNo) {
        this.refNo = refNo;
    }

    public String getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(String responseCode) {
        this.responseCode = responseCode;
    }
}

我解决了托盘和错误。

父级 class

 import org.simpleframework.xml.Element;
    import org.simpleframework.xml.Namespace;
    import org.simpleframework.xml.Root;

    @Root(name = "S:Envelope" ,strict = false)
    @Namespace(prefix = "S", reference = "http://schemas.xmlsoap.org/soap/envelope/")
    public class BuyResponseEnvelope {

        @Element(name = "Body")
        private BuyResponseBody body;

        public BuyResponseBody getBody() {
            return body;
        }

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


    }

正文class

  import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "S:Body" ,strict = false)
public class BuyResponseBody {

    @Element(name = "requestCPResponse")
    private BuyCPResponse cpResponse;


    public BuyCPResponse getCpResponse() {
        return cpResponse;
    }

    public void setCpResponse(BuyCPResponse cpResponse) {
        this.cpResponse = cpResponse;
    }
}

请求 cp class

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;


@Root(name = "ns2:requestCPResponse",strict = false)
@Namespace( prefix = "ns2", reference = "http://incomingwebchannel.hrz.jpos.org/")
public class BuyCPResponse {

    @Element(name = "return")
    private BuyResponseReturn responseReturn;

    public BuyResponseReturn getResponseReturn() {
        return responseReturn;
    }

    public void setResponseReturn(BuyResponseReturn responseReturn) {
        this.responseReturn = responseReturn;
    }
}

和returnclass

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;
import org.simpleframework.xml.Root;

@Root(name = "return",strict = false)
@Namespace( prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance")
public class BuyResponseReturn {


    @Element(name = "refNo")
    private String refNo;

    @Element(name = "responseCode")
    private String responseCode;

    public String getRefNo() {
        return refNo;
    }

    public void setRefNo(String refNo) {
        this.refNo = refNo;
    }

    public String getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(String responseCode) {
        this.responseCode = responseCode;

    }

}