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.
<Request>
<EMPId>?</EMPId>
</Request>
I use the following code to convert the above request string to a Document Object.
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes())));
Once i have the document, i can create SOAPBody by substituting the EMPId values.
Now i have to creare individual SOAPMessages for each SOAPBody created.
For that I use the following 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;
}
But I am getting the following error on executing the line with content 'soapBodyDoc.setPrefix("v1");'
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.
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?