1 57 58 package stockquote.wsiftypes; 59 60 import java.net.URL ; 61 import java.io.*; 62 import org.w3c.dom.*; 63 import org.xml.sax.*; 64 import javax.xml.parsers.*; 65 66 67 74 public class StockQuote { 75 76 public float getQuote (String symbol) throws Exception { 77 if (symbol==null || symbol.length()==0) return -1.0F; 78 79 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 80 DocumentBuilder xdb= factory.newDocumentBuilder(); 81 82 URL url = new URL ("http://www.xmltoday.com/examples/stockquote/getxmlquote.vep?s="+symbol); 86 InputStream is = url.openStream (); 87 Document d = xdb.parse(is); 88 Element e = d.getDocumentElement (); 89 NodeList nl = e.getElementsByTagName ("price"); 90 e = (Element) nl.item (0); 91 String quoteStr = e.getAttribute ("value"); 92 try { 93 return Float.valueOf (quoteStr).floatValue (); 94 } catch (NumberFormatException e1) { 95 try { 97 return Integer.valueOf (quoteStr).intValue () * 1.0F; 98 } catch (NumberFormatException e2) { 99 return -1.0F; 100 } 101 } 102 } 103 } 104 105 | Popular Tags |