1 16 package samples.jaxm; 17 18 import javax.xml.messaging.URLEndpoint; 19 import javax.xml.soap.MessageFactory ; 20 import javax.xml.soap.Name ; 21 import javax.xml.soap.SOAPBody ; 22 import javax.xml.soap.SOAPBodyElement ; 23 import javax.xml.soap.SOAPConnection ; 24 import javax.xml.soap.SOAPConnectionFactory ; 25 import javax.xml.soap.SOAPElement ; 26 import javax.xml.soap.SOAPEnvelope ; 27 import javax.xml.soap.SOAPHeader ; 28 import javax.xml.soap.SOAPMessage ; 29 import javax.xml.soap.SOAPPart ; 30 import java.util.Iterator ; 31 32 public class DelayedStockQuote { 33 public static void main(String [] args) throws Exception { 34 DelayedStockQuote stockQuote = new DelayedStockQuote(); 35 System.out.print("The last price for SUNW is " + stockQuote.getStockQuote("SUNW")); 36 } 37 38 public String getStockQuote(String tickerSymbol) throws Exception { 39 SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance(); 40 SOAPConnection con = scFactory.createConnection(); 41 42 MessageFactory factory = MessageFactory.newInstance(); 43 SOAPMessage message = factory.createMessage(); 44 45 SOAPPart soapPart = message.getSOAPPart(); 46 SOAPEnvelope envelope = soapPart.getEnvelope(); 47 48 SOAPHeader header = envelope.getHeader(); 49 SOAPBody body = envelope.getBody(); 50 51 header.detachNode(); 52 53 Name bodyName = envelope.createName("getQuote", "n", "urn:xmethods-delayed-quotes"); 54 SOAPBodyElement gltp = body.addBodyElement(bodyName); 55 56 Name name = envelope.createName("symbol"); 57 SOAPElement symbol = gltp.addChildElement(name); 58 symbol.addTextNode(tickerSymbol); 59 60 URLEndpoint endpoint = new URLEndpoint("http://64.124.140.30/soap"); 61 SOAPMessage response = con.call(message, endpoint); 62 con.close(); 63 64 SOAPPart sp = response.getSOAPPart(); 65 SOAPEnvelope se = sp.getEnvelope(); 66 SOAPBody sb = se.getBody(); 67 Iterator it = sb.getChildElements(); 68 while (it.hasNext()) { 69 SOAPBodyElement bodyElement = (SOAPBodyElement ) it.next(); 70 Iterator it2 = bodyElement.getChildElements(); 71 while (it2.hasNext()) { 72 SOAPElement element2 = (SOAPElement ) it2.next(); 73 return element2.getValue(); 74 } 75 } 76 return null; 77 } 78 } 79 80 | Popular Tags |