NIjaROcks NIjaROcks - 1 month ago 5
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 = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

// Process the SOAP Response
printSOAPResponse(soapResponse);

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

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

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

should do that.

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