title: SOAP message with javax.xml.soap - namespace error?
body: <p>The following is a generic sample SOAP request for .NET web service I'm supposed to invoke from my java web app:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   &lt;soap:Body&gt;
    &lt;setAMRequestData xmlns="http://tempuri.org/"&gt;
      &lt;id&gt;int&lt;/id&gt;
    &lt;/setAMRequestData&gt;
   &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>I am able to generate something similar from java console application using this code segment:</p>
  
  <pre><code>import javax.xml.XMLConstants;
  import javax.xml.namespace.QName;
  import javax.xml.soap.MessageFactory;
  import javax.xml.soap.SOAPBody;
  import javax.xml.soap.SOAPBodyElement;
  import javax.xml.soap.SOAPConnection;
  import javax.xml.soap.SOAPConnectionFactory;
  import javax.xml.soap.SOAPElement;
  import javax.xml.soap.SOAPHeader;
  import javax.xml.soap.SOAPMessage;
  ...
  SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
  SOAPConnection connection = sfc.createConnection();
  MessageFactory mf = MessageFactory.newInstance();
  SOAPMessage sm = mf.createMessage();
  
  SOAPHeader sh = sm.getSOAPHeader();
  SOAPBody sb = sm.getSOAPBody();
  sh.detachNode();
  
  QName bodyName = new QName("http://tempuri.org/", "setAMRequestData", XMLConstants.DEFAULT_NS_PREFIX);
  SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
  QName n = new QName("id");                                          
  SOAPElement quotation = bodyElement.addChildElement(n);
  quotation.addTextNode("121152");
  </code></pre>
  
  <p>The result is the following XML:</p>
  
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   &lt;SOAP-ENV:Body&gt;
    &lt;setAMRequestData xmlns="http://tempuri.org/"&gt;
     &lt;id&gt;121152&lt;/id&gt;
    &lt;/setAMRequestData&gt;
   &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>This invokes the service. Next, I used <em>soapUI</em> to try to invoke this service, and it generated soap message from WSDL like this (it <strong>differs</strong> from the previous in the namespace declaration in envelope, and prefixes):</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                    xmlns:tem="http://tempuri.org/"&gt;
   &lt;soapenv:Header/&gt;
    &lt;soapenv:Body&gt;
     &lt;tem:setAMRequestData&gt;
       &lt;tem:id&gt;?&lt;/tem:id&gt;
     &lt;/tem:setAMRequestData&gt;
    &lt;/soapenv:Body&gt;
   &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>This also works from soapUI. But finally, when I tried to recreate this form of soap message using this code sequence:</p>
  
  <pre><code>// factories and stuff, like in the example above
  SOAPPart part = sm.getSOAPPart();
  SOAPEnvelope envelope = part.getEnvelope();
  envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");
  
  SOAPHeader sh = sm.getSOAPHeader();
  SOAPBody sb = sm.getSOAPBody();
  sh.detachNode();
  
  QName bodyName = new QName(null, "setAMRequestData", "tem");
  SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
  QName n = new QName(null, "id", "tem");
  SOAPElement quotation = bodyElement.addChildElement(n);
  quotation.addTextNode("7028");
  </code></pre>
  
  <p>I got the following exception in line <em>SOAPElement quotation = bodyElement.addChildElement(n);</em>: </p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>No matter what I tried, I simply couldn't set "tem" prefix for the <em>id</em> element... What is going on here?  </p>
  
  <p>Thanks.</p>