title: Namespace related error on creating SOAP Request
body: <p>My attempt is to create multiple soap messages from the following SOAPBody content[just a sample one, no the actual]. There will be seperate requests for each EmpId.</p>
  
  <pre><code>&lt;Request&gt;
    &lt;EMPId&gt;?&lt;/EMPId&gt;
  &lt;/Request&gt;
  </code></pre>
  
  <p>I use the following code to convert the above request string to a Document Object.</p>
  
  <pre><code>DocumentBuilder parser = factory.newDocumentBuilder();
              Document doc = parser.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes())));
  </code></pre>
  
  <p>Once i have the document, i can create SOAPBody by substituting the EMPId values.</p>
  
  <p>Now i have to creare individual SOAPMessages for each SOAPBody created.</p>
  
  <p>For that I use the following code.</p>
  
  <pre><code>private static String cretaeSOAPMessage(Document soapBodyDoc, String serverURI, String soapAction){
      String soapMsg = null;
  
      try {
          MessageFactory messageFactory = MessageFactory.newInstance();
          SOAPMessage soapMessage = messageFactory.createMessage();
          SOAPPart soapPart = soapMessage.getSOAPPart();
          SOAPEnvelope envelope = soapPart.getEnvelope();
          envelope.addNamespaceDeclaration("v1",serverURI);
  
          SOAPBody soapBody = envelope.getBody();
  
          soapBodyDoc.setPrefix("v1");
          soapBody.addDocument(soapBodyDoc);
  
          MimeHeaders headers = soapMessage.getMimeHeaders();
          headers.addHeader("SOAPAction", serverURI  + soapAction);
  
          soapMessage.saveChanges();
  
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          try {
              soapMessage.writeTo(out);
          } catch (IOException e) {
              e.printStackTrace();
          }
          soapMsg = new String(out.toByteArray());
  
      } catch (SOAPException e) {
          e.printStackTrace();
      }
  
  
      return soapMsg;
  }
  </code></pre>
  
  <p>But I am getting the following error on executing the line with content '<strong>soapBodyDoc.setPrefix("v1");</strong>'</p>
  
  <blockquote>
    <p>Exception in thread "main" 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>I tried to add the namespace prefic where i create the SOAPBody, even that dint worked out.
  How can i avoid this error and add namespace prefix to the SOAPBody?</p>