gpt4 book ai didi

java - 创建 SOAP 消息 : NAMESPACE_ERR An attempt is made to create or change an object in a way which is incorrect with regard to namespaces

转载 作者:行者123 更新时间:2023-11-30 10:43:45 24 4
gpt4 key购买 nike

我正在开发一个 Web 服务客户端,我必须生成这样的代码:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="Envio_ConsultaSecuencia"> 
<soapenv:Header/>
<soapenv:Body>
<env:envio>
<env:cabecera>
<env:idMensaje>ABCDEFG<env:idMensaje>
<env:tipoMensaje>ABCDEFG</env:tipoMensaje>
</env:cabecera>
</env:envio>
</soapenv:Body>
</soapenv:Envelope>

所以,我的问题是当我尝试在 cabecera 中插入前缀“env”时。这是我正在使用的代码:

MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();


SOAPElement envio = body.addChildElement("envio");
envio.setPrefix("env");


SOAPElement cabecera = envio.addChildElement("cabecera");
cabecera.setPrefix("env");
(...)

我不明白为什么我可以在名为“envío”的 SOAPElement 中设置前缀“env”,而当我尝试对“cabecera”做同样的事情时,我得到了这个错误:

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

我会感谢你的帮助。提前致谢。

编辑:

我在 Oracle 的网站上找到了解决方案 https://docs.oracle.com/cd/E19340-01/820-6767/aeqfx/index.html

创建每个 Child 的正确方法是:

Name bodyName = envelope.createName("GetLastTradePrice", "m",
"http://eztrade.com")
SOAPBodyElement gltp = body.addBodyElement(bodyName);

并且前缀插入没有问题。

就是这样!

最佳答案

尝试将命名空间声明添加到 SOAPElement envio 或添加到 SOAPEnvelope。

        SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//add declaration here
envelope.addNamespaceDeclaration("env", "http://som.org");
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();


SOAPElement envio = body.addChildElement("envio");
envio.setPrefix("env");
//explicit declare it here for this element
envio.addNamespaceDeclaration("env", "http://som.org");
SOAPElement cabecera = envio.addChildElement("cabecera","env");

关于java - 创建 SOAP 消息 : NAMESPACE_ERR An attempt is made to create or change an object in a way which is incorrect with regard to namespaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37662187/

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