10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Code Examples<br />

612<br />

import javax.xml.soap.*;<br />

import javax.xml.namespace.QName;<br />

import java.util.Iterator;<br />

import java.net.URL;<br />

public class Request {<br />

public static void main(String[] args) {<br />

try {<br />

SOAPConnectionFactory soapConnectionFactory =<br />

SOAPConnectionFactory.newInstance();<br />

SOAPConnection connection =<br />

soapConnectionFactory.createConnection();<br />

}<br />

}<br />

MessageFactory factory = MessageFactory.newInstance();<br />

SOAPMessage message = factory.createMessage();<br />

SOAPHeader header = message.getSOAPHeader();<br />

SOAPBody body = message.getSOAPBody();<br />

header.detachNode();<br />

QName bodyName = new QName("http://wombat.ztrade.com",<br />

"GetLastTradePrice", "m");<br />

SOAPBodyElement bodyElement = body.addBodyElement(bodyName);<br />

QName name = new QName("symbol");<br />

SOAPElement symbol = bodyElement.addChildElement(name);<br />

symbol.addTextNode("SUNW");<br />

URL endpoint = new URL("http://wombat.ztrade.com/quotes");<br />

SOAPMessage response = connection.call(message, endpoint);<br />

connection.close();<br />

SOAPBody soapBody = response.getSOAPBody();<br />

Iterator iterator = soapBody.getChildElements(bodyName);<br />

bodyElement = (SOAPBodyElement)iterator.next();<br />

String lastPrice = bodyElement.getValue();<br />

System.out.print("<strong>The</strong> last price for SUNW is ");<br />

System.out.println(lastPrice);<br />

} catch (Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!