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

标签 java soap prefix env

我正在开发一个 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/

相关文章:

java - 具有 int 和 char 操作数的三元表达式的类型是什么?

spring-security - 不再需要 Spring Security ROLE_ 前缀?

java - JCO_ERROR_RESOURCE : Destination does not exist

java - Mapstruct - 没有符合条件的 bean 类型

soap - VIES SOAP 给出错误 SERVICE_UNAVAILABLE

python - 通过 Python 和 SUDS 访问 Kashoo API。身份验证 token 中的时间格式问题

javascript - html 属性中的 "javascript:"前缀

r - 为 R 中的所有行添加前缀

java - 扫描仪无法正常工作

java - SOAP url 和 REST url 之间的区别