我正在开发一个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);
插入前缀没有问题。
这就是全部!
答案 0 :(得分:1)
尝试将名称空间声明添加到SOAPElement envio OR到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");