获取雅虎的天气情况

package lee;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

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

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

/**
 * this class is define one method to get weather information by yahoo API
 *
 * @author tatsuya
 *
 */
public class GetWeatherInfo {

 public static final String BEI_JING = "0008";
 public static final String TIAN_JING = "0133";
 public static final String HANG_ZHOU = "0044";
 public static final String HE_FEI = "0448";
 public static final String SHANG_HAI = "0116";
 public static final String FU_ZHOU = "0031";
 public static final String CHONG_QING = "0017";
 public static final String NAN_CHANG = "0097";
 public static final String XIANG_GANG = "0049";
 public static final String JI_NAN = "0064";
 public static final String AO_MEN = "0512";
 public static final String ZHENG_ZHOU = "0165";
 public static final String HU_HE_HAO_TE = "0249";
 public static final String WU_NU_MU_QI = "0135";
 public static final String CHANG_SHA = "0013";
 public static final String YIN_CHUAN = "0259";
 public static final String GUANG_ZHOU = "0037";
 public static final String LA_SA = "0080";
 public static final String HA_KOU = "0502";
 public static final String NAN_NING = "0100";
 public static final String CHENG_DU = "0016";
 public static final String SHI_JIA_ZHUANG = "0122";
 public static final String GUI_YANG = "0039";
 public static final String TAI_YUAN = "0129";
 public static final String KUI_MING = "0076";
 public static final String SHEN_YANG = "0119";
 public static final String XI_AN = "0141";
 public static final String CHANG_CHUN = "0010";
 public static final String LAN_ZHOU = "0079";
 public static final String XI_NING = "0236";
 public static final String NAN_JING = "0099";

 public static void main(String[] args) {
  GetWeatherInfo info = new GetWeatherInfo();
  String weather = info.getWeather(GetWeatherInfo.SHI_JIA_ZHUANG);
  System.out.println(weather);
 }

 /**
  * get weather information by city code
  *
  * @param cityCode
  * @return
  */
 public String getWeather(String cityCode) {
  URL url;
  String result = null;
  try {
   url = new URL("http://weather.yahooapis.com/forecastrss?p=CHXX" + cityCode + "&u=c");
   URLConnection conn = url.openConnection();

   Document doc = stringToElement(conn.getInputStream());
   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 dayNode = nodeMap.getNamedItem("day");
        Node lowNode = nodeMap.getNamedItem("low");
        Node highNode = nodeMap.getNamedItem("high");
        Node textNode = nodeMap.getNamedItem("text");
        Node codeNode = nodeMap.getNamedItem("code");
        String day = "今天";
        if (result == null) {
         result = new String("");
        } else {
         day = "明天";
        }
        result = result + day + " " + switchW(Integer.parseInt(codeNode.getNodeValue())) + " 最低温度:" + lowNode.getNodeValue() + "度 最高温度:" + highNode.getNodeValue() + "度 ";
       }
      }
     }
    }
   }
  } 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;
  }
 }

 private String switchW(int code) {
  String result = null;
  switch (code) {
  case 0:
   result = "龙卷风";
   break;
  case 1:
   result = "热带风暴";
   break;
  case 2:
   result = "飓风";
   break;
  case 3:
   result = "强雷阵雨";
   break;
  case 4:
   result = "雷阵雨";
   break;
  case 5:
   result = "小雨加雪";
   break;
  case 6:
   result = "雨加冰雹";
   break;
  case 7:
   result = "雪加冰雹";
   break;
  case 8:
   result = "冰雨";
   break;
  case 9:
   result = "毛毛雨";
   break;
  case 10:
   result = "冻雨";
   break;
  case 11:
   result = "阵雨";
   break;
  case 12:
   result = "阵雨";
   break;
  case 13:
   result = "小雪";
   break;
  case 14:
   result = "零星小雪";
   break;
  case 15:
   result = "高吹雪";
   break;
  case 16:
   result = "雪";
   break;
  case 17:
   result = "冰雹";
   break;
  case 18:
   result = "雨夹雪";
   break;
  case 19:
   result = "尘";
   break;
  case 20:
   result = "雾";
   break;
  case 21:
   result = "薄雾";
   break;
  case 22:
   result = "多烟的";
   break;
  case 23:
   result = "大风";
   break;
  case 24:
   result = "有风";
   break;
  case 25:
   result = "寒冷";
   break;
  case 26:
   result = "阴天";
   break;
  case 27:
   result = "夜间阴天";
   break;
  case 28:
   result = "白天阴天";
   break;
  case 29:
   result = "夜间部分多云";
   break;
  case 30:
   result = "白天部分多云";
   break;
  case 31:
   result = "夜间清亮";
   break;
  case 32:
   result = "晴朗";
   break;
  case 33:
   result = "转晴";
   break;
  case 34:
   result = "转晴";
   break;
  case 35:
   result = "雨夹冰雹";
   break;
  case 36:
   result = "热";
   break;
  case 37:
   result = "雷阵雨";
   break;
  case 38:
   result = "雷阵雨";
   break;
  case 39:
   result = "雷阵雨";
   break;
  case 40:
   result = "雷阵雨";
   break;
  case 41:
   result = "大雪";
   break;
  case 42:
   result = "阵雪";
   break;
  case 43:
   result = "大雪";
   break;
  case 44:
   result = "多云";
   break;
  case 45:
   result = "雷";
   break;
  case 46:
   result = "阵雪";
   break;
  case 47:
   result = "雷雨";
   break;
  case 3200:
   result = "程序错误";
   break;
  default:
   break;
  }
  return result;
 }

}

你可能感兴趣的:(xml,.net,Yahoo,J#)