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 找個美女做老婆 閱讀(1667) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          公告

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

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 无极县| 揭西县| 兴仁县| 伊吾县| 石林| 正阳县| 谷城县| 五河县| 敦煌市| 沙洋县| 印江| 肥西县| 江山市| 黑水县| 定日县| 德清县| 宜黄县| 许昌市| 双城市| 乌兰浩特市| 宜兴市| 东宁县| 穆棱市| 荃湾区| 崇明县| 玉屏| 漯河市| 文安县| 华坪县| 海盐县| 冕宁县| 呼图壁县| 广西| 武陟县| 凤翔县| 吉木乃县| 房产| 聊城市| 隆昌县| 清水河县| 公主岭市|