Java學習

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

           

          JSP從WebService取天氣預報數據,很精簡的代碼[效果圖]。

          JSP從WebService取天氣預報數據。

          網上也有相關的代碼,不過比較繁瑣,我改進了一下。很簡單地實現了。

          package com.xxx.web;

          import java.io.*;
          import java.net.*;

          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;

          import org.w3c.dom.Document;
          import org.w3c.dom.Node;
          import org.w3c.dom.NodeList;

          public class Weather {
           private static String _url = "

           private Weather() {
           }

           /**
            * @param cityName
            * @return
            */
           private static InputStream getSoapInputStream(String cityName) {
            try {
             /*
              * URL url = new URL(_url + cityName); HttpURLConnection hc =
              * (HttpURLConnection) url.openConnection(); hc.connect();
              * InputStream urlStream = hc.getInputStream(); return urlStream;
              */
             return new URL(_url + cityName).openStream();
            } catch (Exception ex) {
             return null;
            }

           }
           
           /**
            * 用W3C DOM對返回的XML進行解釋
            * @param cityName
            * @return
            */
           public static String getWeatherByCityName(String cityName) {
            try {
             Document doc;
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setNamespaceAware(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             InputStream is = getSoapInputStream(cityName);
             if (is == null){
              return null;
             }
             doc = db.parse(is);
             NodeList nl = doc.getElementsByTagName("string");
             Node n = nl.item(0);
             String weather = n.getFirstChild().getNodeValue();
             is.close();
             return weather;
            } catch (Exception e) {
             e.printStackTrace();
             return null;
            }
           }

           /**
            * @param args
            */
           public static void main(String[] args) {
            System.out.print(getWeatherByCityName("北京"));
           }
          }

          得到一個String對象,剩下的就很好處理了。

          最終實現效果:
          最終實現效果

          posted on 2009-04-03 16:43 找個美女做老婆 閱讀(1666) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          本blog已經搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新乡县| 大足县| 改则县| 昭觉县| 银川市| 湖北省| 阿图什市| 台南县| 宾川县| 山西省| 达州市| 太和县| 华蓥市| 天峻县| 海原县| 伊金霍洛旗| 汉源县| 泽库县| 晋宁县| 广州市| 宜昌市| 忻城县| 信宜市| 方城县| 宣城市| 海门市| 古交市| 昭觉县| 周宁县| 肃宁县| 岳池县| 武宁县| 鲁甸县| 长丰县| 灵武市| 松阳县| 阳西县| 晋宁县| 海淀区| 建德市| 上林县|