乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java獲取yahoo天氣預(yù)報

       fenyu8 2011-07-24

      首先我們需要獲取您需要查詢城市對應(yīng)的代號,我們可以用HashMap來查詢,代碼如下:

      public static HashMap<String, String> cityCode = new HashMap<String, String>();

      /* 初始化城市代號 */
      private void initCitys() {
      cityCode.put(
      "北京", "0008");
      cityCode.put(
      "天津", "0133");
      cityCode.put(
      "武漢", "0138");
      cityCode.put(
      "杭州", "0044");
      cityCode.put(
      "合肥 ", "0448");
      cityCode.put(
      "上海 ", "0116");
      cityCode.put(
      "福州 ", "0031");
      cityCode.put(
      "重慶 ", "0017");
      cityCode.put(
      "南昌 ", "0097");
      cityCode.put(
      "香港 ", "0049");
      cityCode.put(
      "濟(jì)南 ", "0064");
      cityCode.put(
      "澳門 ", "0512");
      cityCode.put(
      "鄭州 ", "0165");
      cityCode.put(
      "呼和浩特 ", "0249");
      cityCode.put(
      "烏魯木齊 ", "0135");
      cityCode.put(
      "長沙 ", "0013");
      cityCode.put(
      "銀川 ", "0259");
      cityCode.put(
      "廣州 ", "0037");
      cityCode.put(
      "拉薩 ", "0080");
      cityCode.put(
      "???", "0502");
      cityCode.put(
      "南京 ", "0100");
      cityCode.put(
      "成都 ", "0016");
      cityCode.put(
      "石家莊 ", "0122");
      cityCode.put(
      "貴陽 ", "0039");
      cityCode.put(
      "太原 ", "0129");
      cityCode.put(
      "昆明 ", "0076");
      cityCode.put(
      "沈陽 ", "0119");
      cityCode.put(
      "西安 ", "0141");
      cityCode.put(
      "長春 ", "0010");
      cityCode.put(
      "蘭州 ", "0079");
      cityCode.put(
      "西寧 ", "0236");
      }

        


      接下來我們要創(chuàng)建鏈接,以獲取天氣預(yù)報的XML文檔

          private Document getWeatherXML(String cityCode) throws IOException {
      URL url
      = new URL("http://weather./forecastrss?p=CHXX"
      + cityCode + "&u=c");
      URLConnection connection
      = url.openConnection();
      Document Doc
      = stringToElement(connection.getInputStream());
      return Doc;
      }

        


      您也可以選擇保存獲取的天氣XML文檔

      /* 保存獲取的天氣信息XML文檔 */
      private void saveXML(Document Doc, String Path) {
      TransformerFactory transFactory
      = TransformerFactory.newInstance();
      Transformer transformer;
      try {
      transformer
      = transFactory.newTransformer();
      DOMSource domSource
      = new DOMSource(Doc);
      File file
      = new File(Path);
      FileOutputStream out
      = new FileOutputStream(file);
      StreamResult xmlResult
      = new StreamResult(out);
      transformer.transform(domSource, xmlResult);
      }
      catch (Exception e) {
      System.out.println(
      "保存文件出錯!");
      }
      }

        


      本人獲取的一份XML保存如下

      <?xml version="1.0" encoding="UTF-8" ?> 
      -
      <rss xmlns:geo="http://www./2003/01/geo/wgs84_pos#" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="2.0">
      -
      <channel>
      <title>Yahoo! Weather - Wuhan, CH</title>
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/Wuhan__CH/*http://weather.yahoo.com/forecast/CHXX0138_c.html</link>
      <description>Yahoo! Weather for Wuhan, CH</description>
      <language>en-us</language>
      <lastBuildDate>Sat, 27 Mar 2010 11:00 pm CST</lastBuildDate>
      <ttl>60</ttl>
      <yweather:location city="Wuhan" country="CH" region="" />
      <yweather:units distance="km" pressure="mb" speed="km/h" temperature="C" />
      <yweather:wind chill="15" direction="110" speed="6.44" />
      <yweather:atmosphere humidity="67" pressure="1015.92" rising="0" visibility="9.99" />
      <yweather:astronomy sunrise="6:19 am" sunset="6:38 pm" />
      -
      <image>
      <title>Yahoo! Weather</title>
      <width>142</width>
      <height>18</height>
      <link>http://weather.yahoo.com</link>
      <url>http://l./a/i/us/nws/th/main_142b.gif</url>
      </image>
      -
      <item>
      <title>Conditions for Wuhan, CH at 11:00 pm CST</title>
      <geo:lat>30.58</geo:lat>
      <geo:long>114.27</geo:long>
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/Wuhan__CH/*http://weather.yahoo.com/forecast/CHXX0138_c.html</link>
      <pubDate>Sat, 27 Mar 2010 11:00 pm CST</pubDate>
      <yweather:condition code="33" date="Sat, 27 Mar 2010 11:00 pm CST" temp="15" text="Fair" />
      -
      <description>
      -
      <!--[CDATA[
      <img src="http://l./a/i/us/we/52/33.gif" mce_src="http://l./a/i/us/we/52/33.gif"/><br />
      <b>Current Conditions:</b><br />
      Fair, 15 C<BR />
      <BR /><b>Forecast:</b><BR />
      Sat - Partly Cloudy. High: 18 Low: 9<br />
      Sun - Partly Cloudy. High: 20 Low: 12<br />
      <br />
      <a mce_>Full Forecast at Yahoo! Weather</a><BR/><BR/>
      (provided by <a href="http://www." mce_href="http://www." >The Weather Channel</a>)<br/>

      ]]
      -->
      </description>
      <yweather:forecast code="29" date="27 Mar 2010" day="Sat" high="18" low="9" text="Partly Cloudy" />
      <yweather:forecast code="30" date="28 Mar 2010" day="Sun" high="20" low="12" text="Partly Cloudy" />
      <guid isPermaLink="false">CHXX0138_2010_03_27_23_00_CST</guid>
      </item>
      </channel>
      </rss>
      -
      <!-- api7.weather.sp1.yahoo.com uncompressed/chunked Sat Mar 27 08:43:16 PDT 2010
      -->

        


      好啦,下面就是解析XML了,您看一下XML文檔,如果您了解的話很容易獲取其中的信息

          /* 獲取天氣信息 */
      public String getWeather(String city) {
      String result
      = null;
      try {
      Document doc
      = getWeatherXML(GetWeatherInfo.cityCode.get(city));
      NodeList nodeList
      = doc.getElementsByTagName("channel");
      for (int i = 0; i < nodeList.getLength(); i++) {
      Node node
      = nodeList.item(i);
      NodeList nodeList1
      = node.getChildNodes();
      for (int j = 0; j < nodeList1.getLength(); j++) {
      Node node1
      = nodeList1.item(j);
      if (node1.getNodeName().equalsIgnoreCase("item")) {
      NodeList nodeList2
      = node1.getChildNodes();
      for (int k = 0; k < nodeList2.getLength(); k++) {
      Node node2
      = nodeList2.item(k);
      if (node2.getNodeName().equalsIgnoreCase(
      "yweather:forecast")) {
      NamedNodeMap nodeMap
      = node2.getAttributes();
      Node lowNode
      = nodeMap.getNamedItem("low");
      Node highNode
      = nodeMap.getNamedItem("high");
      Node codeNode
      = nodeMap.getNamedItem("code");
      String day
      = "今天";
      if (result == null) {
      result
      = "";
      }
      else {
      day
      = "\n明天";
      }
      result
      = result
      + day
      + " "
      + dictionaryStrings[Integer
      .parseInt(codeNode
      .getNodeValue())]
      + "\t最低溫度:" + lowNode.getNodeValue()
      + "℃ \t最高溫度:" + highNode.getNodeValue()
      + "";
      }
      }
      }
      }
      }
      saveXML(doc,
      "C:\\Users\\ygui\\Desktop\\Weather.xml");
      }
      catch (Exception e) {
      e.printStackTrace();
      }
      return result;
      }

        


      整個程序的代碼如下:

      package stwolf.hustbaidu.java.learn.filelist;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.URL;
      import java.net.URLConnection;
      import java.util.HashMap;
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamResult;
      import org.w3c.dom.Document;
      import org.w3c.dom.NamedNodeMap;
      import org.w3c.dom.Node;
      import org.w3c.dom.NodeList;
      class GetWeatherInfo {
      public static HashMap<String, String> cityCode = new HashMap<String, String>();
      private final String[] dictionaryStrings = { "龍卷風(fēng)", "熱帶風(fēng)暴", "颶風(fēng)", "強(qiáng)雷陣雨",
      "雷陣雨", "小雨加雪", "雨加冰雹", "雪加冰雹", "冰雨", "毛毛雨", "凍雨", "陣雨", "陣雨", "小雪",
      "零星小雪", "高吹雪", "", "冰雹", "雨夾雪", "", "", "薄霧", "多煙的", "大風(fēng)", "有風(fēng)",
      "寒冷", "陰天", "夜間陰天", "白天陰天", "夜間多云", "白天多云", "夜間清亮", "晴朗", "轉(zhuǎn)晴",
      "轉(zhuǎn)晴", "雨夾冰雹", "", "雷陣雨", "雷陣雨", "雷陣雨", "雷陣雨", "大雪", "陣雪", "大雪",
      "多云", "", "陣雪", "雷雨" };
      public GetWeatherInfo() {
      initCitys();
      }
      /* 初始化城市代號 */
      private void initCitys() {
      cityCode.put(
      "北京", "0008");
      cityCode.put(
      "天津", "0133");
      cityCode.put(
      "武漢", "0138");
      cityCode.put(
      "杭州", "0044");
      cityCode.put(
      "合肥 ", "0448");
      cityCode.put(
      "上海 ", "0116");
      cityCode.put(
      "福州 ", "0031");
      cityCode.put(
      "重慶 ", "0017");
      cityCode.put(
      "南昌 ", "0097");
      cityCode.put(
      "香港 ", "0049");
      cityCode.put(
      "濟(jì)南 ", "0064");
      cityCode.put(
      "澳門 ", "0512");
      cityCode.put(
      "鄭州 ", "0165");
      cityCode.put(
      "呼和浩特 ", "0249");
      cityCode.put(
      "烏魯木齊 ", "0135");
      cityCode.put(
      "長沙 ", "0013");
      cityCode.put(
      "銀川 ", "0259");
      cityCode.put(
      "廣州 ", "0037");
      cityCode.put(
      "拉薩 ", "0080");
      cityCode.put(
      "???", "0502");
      cityCode.put(
      "南京 ", "0100");
      cityCode.put(
      "成都 ", "0016");
      cityCode.put(
      "石家莊 ", "0122");
      cityCode.put(
      "貴陽 ", "0039");
      cityCode.put(
      "太原 ", "0129");
      cityCode.put(
      "昆明 ", "0076");
      cityCode.put(
      "沈陽 ", "0119");
      cityCode.put(
      "西安 ", "0141");
      cityCode.put(
      "長春 ", "0010");
      cityCode.put(
      "蘭州 ", "0079");
      cityCode.put(
      "西寧 ", "0236");
      }
      private Document getWeatherXML(String cityCode) throws IOException {
      URL url
      = new URL("http://weather./forecastrss?p=CHXX"
      + cityCode + "&u=c");
      URLConnection connection
      = url.openConnection();
      Document Doc
      = stringToElement(connection.getInputStream());
      return Doc;
      }
      /* 保存獲取的天氣信息XML文檔 */
      private void saveXML(Document Doc, String Path) {
      TransformerFactory transFactory
      = TransformerFactory.newInstance();
      Transformer transformer;
      try {
      transformer
      = transFactory.newTransformer();
      DOMSource domSource
      = new DOMSource(Doc);
      File file
      = new File(Path);
      FileOutputStream out
      = new FileOutputStream(file);
      StreamResult xmlResult
      = new StreamResult(out);
      transformer.transform(domSource, xmlResult);
      }
      catch (Exception e) {
      System.out.println(
      "保存文件出錯!");
      }
      }
      /* 獲取天氣信息 */
      public String getWeather(String city) {
      String result
      = null;
      try {
      Document doc
      = getWeatherXML(GetWeatherInfo.cityCode.get(city));
      NodeList nodeList
      = doc.getElementsByTagName("channel");
      for (int i = 0; i < nodeList.getLength(); i++) {
      Node node
      = nodeList.item(i);
      NodeList nodeList1
      = node.getChildNodes();
      for (int j = 0; j < nodeList1.getLength(); j++) {
      Node node1
      = nodeList1.item(j);
      if (node1.getNodeName().equalsIgnoreCase("item")) {
      NodeList nodeList2
      = node1.getChildNodes();
      for (int k = 0; k < nodeList2.getLength(); k++) {
      Node node2
      = nodeList2.item(k);
      if (node2.getNodeName().equalsIgnoreCase(
      "yweather:forecast")) {
      NamedNodeMap nodeMap
      = node2.getAttributes();
      Node lowNode
      = nodeMap.getNamedItem("low");
      Node highNode
      = nodeMap.getNamedItem("high");
      Node codeNode
      = nodeMap.getNamedItem("code");
      String day
      = "今天";
      if (result == null) {
      result
      = "";
      }
      else {
      day
      = "\n明天";
      }
      result
      = result
      + day
      + " "
      + dictionaryStrings[Integer
      .parseInt(codeNode
      .getNodeValue())]
      + "\t最低溫度:" + lowNode.getNodeValue()
      + "℃ \t最高溫度:" + highNode.getNodeValue()
      + "";
      }
      }
      }
      }
      }
      saveXML(doc,
      "C:\\Users\\ygui\\Desktop\\Weather.xml");
      }
      catch (Exception e) {
      e.printStackTrace();
      }
      return result;
      }
      public Document stringToElement(InputStream input) {
      try {
      DocumentBuilder db
      = DocumentBuilderFactory.newInstance()
      .newDocumentBuilder();
      Document doc
      = db.parse(input);
      return doc;
      }
      catch (Exception e) {
      return null;
      }
      }
      }
      public class Test {
      public static void main(String arg[]) {
      GetWeatherInfo info
      = new GetWeatherInfo();
      String weather
      = info.getWeather("武漢");
      System.out.println(weather);
      }
      }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多