我正在开发一个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.
我会感谢您的帮助。提前谢谢。
编辑:
我在甲骨文的网络 https://docs.oracle.com/cd/E19340-01/820-6767/aeqfx/index.html 中找到了解决方案。
创建每个子项的正确方法是:
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");