如何将XML SOAP String转换为JSONObject(com.ibm.json.java.JSONObject)

时间:2017-04-04 07:20:24

标签: json xml

美好的一天,

我在Adapter IBM Mobilefirst Platform中创建了一个java类,它将从Jax-ws服务获得响应。


     // 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。此代码将获取XML字符串中的Response,但我不知道如何使用库com.ibm.json。*。我想将String Response转换为JSONObject。

2.1。 XML Soap Envelope中的结果响应(示例成功结果)。

<?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我需要将字符串XML Soap响应转换为JSONObject

的字符串变量

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

1 个答案:

答案 0 :(得分:0)

我回答我的问题:

最近我只是尝试,我需要使用此方法XMLToJSONTransformer(com.ibm.json.xml.XMLToJSONTransformer)将XML字符串转换为JSONObject。

String jsonObjectStr ="";

// convert String to JSONObject
jsonObjectStr = xmlToJsonTransformer.transform(responseSoap);
        jsonObj = JSONObject.parse(jsonObjectStr);