NIjaROcks NIjaROcks - 1 year ago 121
Java Question

Adding XML decleration to soap message

I am trying to add a XML Declaration for ex:

<?xml version="1.0" encoding="utf-8"?>
to the top of this soap message. Can anyone assist me on how this can be done?

try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server
String url = "";
SOAPMessage soapResponse =, url);

// Process the SOAP Response

} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");

private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPHeader header = soapMessage.getSOAPHeader();

Answer Source
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION ,Boolean.TRUE)

should do that.

Here is the link to setProperty(String property, Object value) documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download