Friday, 29 April 2011

SOAP Messaging End to End

Create the message object. This can be done using MessageFactory. Message object is required to get the header and body object.
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
If you do not need SOAP header then in that case you can simply delete the header using detach node method else skip this part.
SOAPHeader header = message.getSOAPHeader();
Create the body element for the API you want to access. In this case GetConvertedValue is called to get the USD:EUR value.
QName bodyName = new QName("", "GetConvertedValue");
SOAPBody body = message.getSOAPBody();
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
Creating 'From' element for USD:
QName fromQName = new QName("From");
SOAPElement fromElement = bodyElement.addChildElement(fromQName);
Creating 'To' element for EUR:
QName toQName = new QName("To");
SOAPElement toElement = bodyElement.addChildElement(toQName);
Define the end-point URL in order to establish the connection.
URL endpoint = new URL("");
Create a SOAP connection object. This can be created using SOAP connection factory. Its a direct connection between client and server.
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
Connect to the server and get response.Close the connection after use.
SOAPMessage response =, endpoint);
Get the body of the response from SOAP message.
SOAPBody soapBody = response.getSOAPBody();
Finally iterate to get the USD to EUR exchange rate.
Iterator iterator = soapBody.getChildElements(bodyName);
bodyElement = (SOAPBodyElement);
String convertedValue = bodyElement.getValue();

No comments:

Post a Comment