Java 如何使用 CDATA 创建 SOAP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24029081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:47:23  来源:igfitidea点击:

How to create SOAP request with CDATA

javaweb-servicessoapcdata

提问by LMK

I am new to SOAP,I want to create SOAP request,as given below

我是 SOAP 新手,我想创建 SOAP 请求,如下所示

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
 <SOAP-ENV:Header/>
<soapenv:Body>
  <tem:RequestData>
     <tem:requestDocument>
        <![CDATA[
        <Request>
           <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
           <Establishment Id="4297867"/>
        </Request>
        ]]>

     </tem:requestDocument>
  </tem:RequestData>
</soapenv:Body>
</soapenv:Envelope>  

Code for creating SOAP request i have found in tutorial

我在教程中找到的用于创建 SOAP 请求的代码

  MessageFactory mf = MessageFactory.newInstance();
                    SOAPMessage sm = mf.createMessage();
                    SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
                    envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
                    envelope.setPrefix("soapenv");

                    envelope.setAttribute("xmlns:tem", "http://tempuri.org/");       

                    SOAPBody body = envelope.getBody();
                    body.setPrefix("soapenv");

                    SOAPElement requestData = body.addChildElement("RequestData");
                    requestData.setPrefix("tem");

                    SOAPElement requestDoc = requestData.addChildElement("requestDocument","tem","http://tempuri.org/");

                    requestDoc.addTextNode(" <![CDATA[");

                    SOAPElement request = requestDoc.addChildElement("Request");

                    SOAPElement authentication = request.addChildElement("Authentication");
                    authentication.setAttribute("CMId", "68");
                    authentication.setAttribute("Guid", "5594FB83-F4D4-431F-B3C5-EA6D7A8BA795");
                    authentication.setAttribute("Password", "poihg321TR");
                    authentication.setAttribute("Function", "1");

                     SOAPElement establishment = request.addChildElement("Establishment");
                        establishment.setAttribute("Id", "4297867");
                     requestDoc.addTextNode("]]>");

                    System.out.println("\nSoap Request: \n\n\n"+getMsgAsString(sm));  

Output I am getting is when executing the code.

我得到的输出是执行代码时。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><SOAP-ENV:Header/><soapenv:Body><tem:RequestData><tem:requestDocument xmlns:tem="http://tempuri.org/"> &lt;![CDATA[<Request><Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/><Establishment Id="4297867"/></Request>]]&gt;</tem:requestDocument></tem:RequestData></soapenv:Body></soapenv:Envelope>  

The problem is

问题是

<![CDATA[     ]]>  is displaying like  &lt;![CDATA[ and ]]&gt;  

And my Server is not accepting this request.

我的服务器不接受这个请求。

采纳答案by helderdarocha

You need to create and add a CDATASection:

您需要创建并添加一个CDATASection

CDATASection cdata = 
    requestDoc.getOwnerDocument().createCDATASection("<element>text</element>");
requestDoc.appendChild(cdata);

This will produce:

这将产生:

<![CDATA[<element>text</element>]]>