1

I need to convert a soap response xml to a JAVA object to cater it to another service. SOAP response looks like

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:myResponse xmlns:ns2="http://impl.service.abc.com/">
         <return>
            <response>
               <timestamp>11068446</timestamp>
               <txnAmount>1</txnAmount>
               <userGuid>11068446</userGuid>
               <walletSystemTxnId>123456789</walletSystemTxnId>
            </response>
            <status>SUCCESSS</status>
            <statusCode>SUCCESS</statusCode>
            <statusMessage>SUCCESS</statusMessage>
         </return>
      </ns2:myResponse>
   </S:Body>
</S:Envelope>

I tried lots f things to convert read it and convert it to my class instance.

How I am calling the soapservice. I am calling soapService which is using webService template of spring framework. code is

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();
    //Send SOAP Message to SOAP Server
   SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapWithdrawAction), soapEndpointUrl);

when I am trying to print the soapResponse I am getting, the xml which is posted in the very beginning of the question. Is there any way to achieve this?

Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48

2 Answers2

0

Can u try like below ! Hope it will help

Create the MyResponse class with the help of wsimport with required XSD

@Autowired
@Qualifier("repositoryWsTemplate")
private WebServiceTemplate repositoryWsTemplate;
 public Object executeSoapReqAndRes(Object object,WebServiceMessageCallback requestCallback){
MyResponse myResponseInstance =(MyResponse )repositoryWsTemplate
            .marshalSendAndReceive(object, requestCallback);
return myResponseInstance ;
}

Note: Hope you have used wsdl to generate the MyResponse class.If not please do that. you can refer the below one two generate java class. JAXB - Mapping SOAP to Java Classes

-1
int PRETTY_PRINT_INDENT_FACTOR = 4;
String TEST_XML_STRING =
            "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";

try {
     JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
     String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
     System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
     System.out.println(je.toString());
}

hope this will help you, use org.json jar file

Gokul raj
  • 53
  • 5
  • if you want more info refer this link https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java – Gokul raj May 21 '18 at 11:22