home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wsgatsam.zip / Stockquote.java < prev    next >
Text File  |  2003-02-24  |  2KB  |  59 lines

  1. /**
  2.  * %wsgw_sample_start%
  3.  * Licensed Materials - Property of IBM  
  4.  *    
  5.  * (c) Copyright IBM Corp. 2001, 2002 All Rights Reserved.  
  6.  *    
  7.  * US Government Users Restricted Rights - Use, duplication or   
  8.  * disclosure restricted by GSA ADP Schedule Contract with   
  9.  * IBM Corp.  
  10.  * %wsgw_sample_end%
  11.  */
  12. package services.stockquote;
  13.  
  14. import java.net.URL;
  15. import java.io.*;
  16. import org.w3c.dom.*;
  17. import org.xml.sax.*;
  18. import javax.xml.parsers.*;
  19. import org.apache.soap.util.xml.*;
  20.  
  21.  
  22. /**
  23.  * This sample is taken from ApacheSOAP and changed to get
  24.  *  JAXP document builder directly (no need for Apache SOAP classes).
  25.  *
  26.  * @author Alekander Slominski (aslom@watson.ibm.com)
  27.  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
  28.  */
  29. public class Stockquote {
  30.   
  31.   public float getQuote (String symbol) throws Exception {
  32.     
  33.     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  34.     DocumentBuilder xdb= factory.newDocumentBuilder();
  35.     
  36.     // get a real (delayed by 20min) stockquote from
  37.     // http://www.xmltoday.com/examples/stockquote/. The IP addr
  38.     // below came from the host that the above form posts to ..
  39.     URL url = new URL ("http://www.xmltoday.com/examples/stockquote/getxmlquote.vep?s="+symbol);
  40.     InputStream is = url.openStream ();
  41.     Document d = xdb.parse(is);
  42.     Element e = d.getDocumentElement ();
  43.     NodeList nl = e.getElementsByTagName ("price");
  44.     e = (Element) nl.item (0);
  45.     String quoteStr = e.getAttribute ("value");
  46.     try {
  47.       return Float.valueOf (quoteStr).floatValue ();
  48.     } catch (NumberFormatException e1) {
  49.       // mebbe its an int?
  50.       try {
  51.         return Integer.valueOf (quoteStr).intValue () * 1.0F;
  52.       } catch (NumberFormatException e2) {
  53.         return -1.0F;
  54.       }
  55.     }
  56.   }
  57. }
  58.  
  59.