Java 如何使用CDATA创建SOAP请求

Java 如何使用CDATA创建SOAP请求,java,web-services,soap,cdata,Java,Web Services,Soap,Cdata,我是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新手,我想创建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>  

]]>
我在教程中找到了创建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));  
MessageFactory mf=MessageFactory.newInstance();
SOAPMessage sm=mf.createMessage();
SOAPEnvelope信封=sm.getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration(“soap”http://schemas.xmlsoap.org/soap/envelope/");
信封。设置前缀(“soapenv”);
setAttribute(“xmlns:tem”http://tempuri.org/");       
SOAPBody=envelope.getBody();
body.setPrefix(“soapenv”);
SOAPElement requestData=body.addChildElement(“requestData”);
requestData.setPrefix(“tem”);
SOAPElement requestDoc=requestData.addChildElement(“requestDocument”、“tem”和http://tempuri.org/");
requestDoc.addTextNode(“”);
System.out.println(“\nSoap请求:\n\n\n”+GetMsGaString(sm));
我得到的输出是在执行代码时

<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>  
![CDATA[]
问题是

<![CDATA[     ]]>  is displaying like  &lt;![CDATA[ and ]]&gt;  
显示得像![CDATA[和]]

而且我的服务器不接受此请求。

您需要创建并添加一个
CDATA节

CDATASection cdata = 
    requestDoc.getOwnerDocument().createCDATASection("<element>text</element>");
requestDoc.appendChild(cdata);
cdata节cdata=
requestDoc.getOwnerDocument().createCDATA节(“文本”);
requestDoc.appendChild(cdata);
这将产生:

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