0
美好的一天,如何XML SOAP字符串轉換爲JSONObject的(com.ibm.json.java.JSONObject)
我已經創建了適配器IBM Mobilefirst平臺的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字符串中的響應,但我不知道如何使用庫com.ibm.json。*。我想將字符串響應轉換爲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