1 55 56 package customfactory.service; 57 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.NodeList ; 61 62 import javax.xml.parsers.DocumentBuilder ; 63 import javax.xml.parsers.DocumentBuilderFactory ; 64 import java.net.URL ; 65 66 71 public class StockQuoteService { 72 public float getQuote (String symbol) throws Exception { 73 77 if ( symbol.equals("XXX") ) return( (float) 55.25 ); 78 79 URL url = new URL ( "http://services.xmethods.net/" + 80 "axis/getQuote?s="+symbol ); 81 82 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 83 DocumentBuilder db = dbf.newDocumentBuilder(); 84 85 Document doc = db.parse( url.toExternalForm() ); 86 Element elem = doc.getDocumentElement(); 87 NodeList list = elem.getElementsByTagName( "stock_quote" ); 88 89 if ( list != null && list.getLength() != 0 ) { 90 elem = (Element ) list.item(0); 91 list = elem.getElementsByTagName( "price" ); 92 elem = (Element ) list.item(0); 93 String quoteStr = elem.getAttribute("value"); 94 try { 95 return Float.valueOf(quoteStr).floatValue(); 96 } catch (NumberFormatException e1) { 97 try { 99 return Integer.valueOf(quoteStr).intValue() * 1.0F; 100 } catch (NumberFormatException e2) { 101 return -1.0F; 102 } 103 } 104 } 105 return( 0 ); 106 } 107 } 108 | Popular Tags |