I'm developing a Web Service client and I hace to generate a code like this one:

<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>

So, my problem is when I try to insert the prefix "env" at cabecera. This is the code I'm using:

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");
        (...)

I don't understand why I can set the prefix "env" in the SOAPElement named "envío" and when I'm trying to do the same with "cabecera" I got this error:

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.

I'll apreciete your help. Thanks in advance.

EDIT:

I found the solution in Oracle's web https://docs.oracle.com/cd/E19340-01/820-6767/aeqfx/index.html

The correct way to create each Child is:

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

And the prefix is inserted without problems.

That's all!

1

There are 1 best solutions below

0
divyank On

Try to add namespace declaration to SOAPElement envio OR to the 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");