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

fndong

再会,

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


     // 直接读取soap服务 - 4/4/2017 
        protected JSONObject createJsonObjectFrmSOAPRequest(Map mapsData) throws IOException, SOAPException { 
            JSONObject jsonObj = new JSONObject(); 
            字符串 responseSoap=""; 
            // 创建 SOAP 连接
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
            SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 
            try { 
            // 向 SOAP 服务器发送 SOAP 消息
            String url = "http://XXXXX:001/test-pvr-ws/empl_get"; 
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(mapsData), url);

            // 处理 SOAP 响应
            responseSoap = printSOAPResponse(soapResponse); 

            // 如何转换为jsonobject,输出为xml字符串
            // XMLToJSONTransformer.transform(responseSoap); - 不知道如何使用 XMLToJSONTransformer 
            //JSONObject.parse(responseSoap); // 将字符串转换为 JSONObject 


            logger.info("jsonObject : " + jsonObj); 
            } catch (Exception e) { 
                // TODO 自动生成的 catch 块
                e.printStackTrace(); 
            }

            返回jsonObj; 
        }
            尝试 {

 
        protected String printSOAPResponse(SOAPMessage soapResponse) 抛出异常 {
            String finalstringEnv = ""; 


                TransformerFactory TransformerFactory = TransformerFactory.newInstance(); 
                变压器变压器=transformerFactory.newTransformer(); 
                源 sourceContent = soapResponse.getSOAPPart().getContent(); 
                System.out.print("\nResponse SOAP Message = "); 
                //为输出
                StringWriter创建一个 StringWriter outWriter = new StringWriter(); 
             // StreamResult 结果 = new StreamResult(System.out); // 这是用于打印行输出
                StreamResult result = new StreamResult(outWriter); 
                转换器.transform(sourceContent, result);
             // 如何将 Transformer.transform() 转换为 String java  
                StringBuffer sb = outWriter.getBuffer();
                finalstringEnv = sb.toString(); 


            } catch (Exception e) { 
                // TODO: 处理异常
                e.printStackTrace(); 
            }



        返回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


    // 处理 SOAP 响应
      responseSoap = printSOAPResponse(soapResponse); 
    //................ 将字符串 XML Soap 响应转换为 JSONObject 的代码

fndong

我回答我的问题:

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

String jsonObjectStr ="";

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章