gpt4 book ai didi

java - 如何使用 CDATA 创建 SOAP 请求

转载 作者:搜寻专家 更新时间:2023-11-01 01:34:28 25 4
gpt4 key购买 nike

我是 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));

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

<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[     ]]>  is displaying like  &lt;![CDATA[ and ]]&gt;  

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

最佳答案

您需要创建并添加一个CDATASection:

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

这将产生:

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

关于java - 如何使用 CDATA 创建 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com