I am able to generate a SOAP message but I do not know
add prefix only to soapMessage tag (should not have namespace)
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage message = factory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPBody body = soapEnvelope.getBody(); soapEnvelope.removeNamespaceDeclaration(soapEnvelope.getPrefix()); soapEnvelope.setPrefix("soap"); body.setPrefix("soap"); header.removeNamespaceDeclaration(header.getPrefix()); header.setPrefix("soap"); soapEnvelope.addNamespaceDeclaration("v9", "URL TO SERVER"); Name bodyName; bodyName = soapFactory.createName("SearchHotels"); SOAPBodyElement getList = body.addBodyElement(bodyName); getList.setPrefix("v9"); Name childName = soapFactory.createName("SoapMessage", "v9", "URL TO SERVER"); SOAPElement HotelListRequest = getList.addChildElement(childName); HotelListRequest.addChildElement("Hotel", "v9").addTextNode("Hilton");
My SOAP Message
...
<v9:SoapMessage xmlns:els="URL TO SERVER">
...
What I expect
...
<v9:SoapMessage>
...
Update :
If I use the following it runs into following error
SOAPElement HotelListRequest = getList.addChildElement("v9:SoapMessage");
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.