1

Good Day,

I have created a java class in Adapter IBM Mobilefirst Platform that will get response from the Jax-ws service.

     // read direct to soap service - 4/4/2017
        protected JSONObject createJsonObjectFrmSOAPRequest(Map mapsData) throws IOException, SOAPException {
            JSONObject jsonObj = new JSONObject();
            String responseSoap="";
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            try {
            // Send SOAP Message to SOAP Server
            String url = "http://XXXXX:001/test-pvr-ws/empl_get";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(mapsData), url);

            // Process the SOAP Response
            responseSoap = printSOAPResponse(soapResponse);

            // how to convert to jsonobject, the output is in xml string
            //  XMLToJSONTransformer.transform(responseSoap); - don't know how to use XMLToJSONTransformer
            //JSONObject.parse(responseSoap); // convert String to JSONObject


            logger.info("jsonObject : " + jsonObj);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return jsonObj;
        }


        protected String printSOAPResponse(SOAPMessage soapResponse) throws Exception {
            String finalstringEnv = "";

            try {

                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                Source sourceContent = soapResponse.getSOAPPart().getContent();
                System.out.print("\nResponse SOAP Message = ");
                //create a StringWriter for the output
                StringWriter outWriter = new StringWriter();
             // StreamResult result = new StreamResult(System.out); // this is for print line output
                StreamResult result = new StreamResult(outWriter);
                transformer.transform(sourceContent, result);
             // how to convert Transformer.transform() to String java
                StringBuffer sb = outWriter.getBuffer(); 
                finalstringEnv = sb.toString();


            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }



        return finalstringEnv;
        }

2 . This code will get the Response in XML string, but I don't know how to use library com.ibm.json.*. Which I wanted to convert the String Response to JSONObject.

2.1. Result Response in XML Soap Envelope (example successful Result i got).

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
    <a:getTheResponse xmlns:a="http://XXXXX:001/test-pvr-ws/empl_get/">
    <getTheRecord>
    <statusCode>0</statusCode>
    <getTheRecord>
    <userid>1212</userid>
    </a:getTheResponse>
    </soapenv:Body>
    </soapenv:Envelope>

2.2. responseSoap String variable i need to convert String XML Soap response To JSONObject

    // Process the SOAP Response
      responseSoap = printSOAPResponse(soapResponse);
    //.............. code to convert String XML Soap response To  JSONObject

1
  • if I may, what requires the conversion to JSON? Isn't the XML richer? Commented Jan 4, 2019 at 15:37

1 Answer 1

0

I answer my Question:

Recently I just trying , I need to use this method XMLToJSONTransformer (com.ibm.json.xml.XMLToJSONTransformer) to convert the XML string to JSONObject.

String jsonObjectStr ="";

// convert String to JSONObject
jsonObjectStr = xmlToJsonTransformer.transform(responseSoap);
        jsonObj = JSONObject.parse(jsonObjectStr); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.