When I create my SOAP request using the javax.xml.soap.SOAPMessage
class, I get a namespace prefix in my XML like <SOAP-ENV:Envelope>
.
Is there a simple way to change the namespace prefix to <soapenv:Envelope>
?
Advertisement
Answer
The XML namespace prefix should not matter as long as you are calling a well behaved web service. So SOAP-ENV:
, soapenv:
, or whatever:
are basically the same thing as long as they both refer to the same namespace.
With that being said, if you really need to do this for some reason, here is some minimally working code. I’m calling this service.
package soap; import java.io.IOException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; 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.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; public class Client { public static void main(String[] args) throws SOAPException, IOException { MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); MimeHeaders mimeHeader = message.getMimeHeaders(); mimeHeader.setHeader("SOAPAction", "http://tempuri.org/Add"); SOAPBody body = message.getSOAPBody(); SOAPHeader header = message.getSOAPHeader(); QName bodyName = new QName("http://tempuri.org/", "Add", "tmp"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); SOAPElement intA = bodyElement.addChildElement(new QName("http://tempuri.org/", "intA", "tmp")); intA.addTextNode("123"); SOAPElement intB = bodyElement.addChildElement(new QName("http://tempuri.org/", "intB", "tmp")); intB.addTextNode("456"); message.writeTo(System.out); System.out.println(); // -----------------> Now changing the prefix before making the call SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); envelope.removeNamespaceDeclaration(envelope.getPrefix()); String prefix = "soapenv"; envelope.addNamespaceDeclaration(prefix, "http://schemas.xmlsoap.org/soap/envelope/"); envelope.setPrefix(prefix); header.setPrefix(prefix); body.setPrefix(prefix); // <---------------- done changing the prefix message.writeTo(System.out); System.out.println(); SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); URL endpoint = new URL("http://www.dneonline.com/calculator.asmx"); SOAPMessage response = connection.call(message, endpoint); connection.close(); response.writeTo(System.out); } }
I’ve printed the request message before and after the transformation. The output is as follows (formatting not included :D):
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <tmp:Add xmlns:tmp="http://tempuri.org/"> <tmp:intA>123</tmp:intA> <tmp:intB>456</tmp:intB> </tmp:Add> </SOAP-ENV:Body> </SOAP-ENV:Envelope> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <tmp:Add xmlns:tmp="http://tempuri.org/"> <tmp:intA>123</tmp:intA> <tmp:intB>456</tmp:intB> </tmp:Add> </soapenv:Body> </soapenv:Envelope> <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <AddResponse xmlns="http://tempuri.org/"> <AddResult>579</AddResult> </AddResponse> </soap:Body> </soap:Envelope>