java类获取天气预报信息

网上有很多通过iframe的形式来显示其他网站上的天气预报,这样有一点不好,没有自己的风格,如果通过类来读取这些天气信息,然后我们就可以在页面上构造自己想要的效果和风格。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class GetWeather {

public String getweather(String city)
{
URL ur;
try {
ur = new URL("http://www.google.com/ig/api?hl=zh_cn&weather="+city);
InputStream instr = ur.openStream();
String s, str;
BufferedReader in = new BufferedReader(new InputStreamReader(instr));
StringBuffer sb = new StringBuffer();
Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("weather.txt"),"utf-8"));
while ((s = in.readLine()) != null) {
sb.append(s);
}
str = new String(sb);
out.write(str);
out.close();
in.close();
} catch (Exception e1) {
e1.printStackTrace();
}
File f=new File("weather.txt");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
String str=null;
try{
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl =(NodeList) doc.getElementsByTagName("forecast_conditions");
NodeList n2=nl.item(0).getChildNodes();
str=n2.item(4).getAttributes().item(0).getNodeValue()+","+n2.item(1).getAttributes().item(0).getNodeValue()+"℃-"+n2.item(2).getAttributes().item(0).getNodeValue()+"℃";
}catch(Exception e)
{
e.printStackTrace();
}
return str;
}
public static void main(String [] arg)
{
//注意weather那写入城市的拼音转化一下就行打开之后是XML格式的然后再提取
System.out.println(new GetWeather().getweather("zhuzhou"));//查询株洲的天气信息
}

}


XML文件如下,这文件是从http://www.google.com/ig读取到的
<?xml version="1.0"?>
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
<forecast_information>
<city data="Zhuzhou, Hunan"/>
<postal_code data="zhuzhou"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2009-10-22"/>
<current_date_time data="2009-10-22 23:00:00 +0000"/>
<unit_system data="SI"/>
</forecast_information>
<current_conditions>
<condition data="多云"/>
<temp_f data="72"/>
<temp_c data="22"/>
<humidity data="湿度: 47%"/>
<icon data="/ig/images/weather/mostly_cloudy.gif"/>
<wind_condition data="风向: 西、风速:2 米/秒"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="周四"/>
<low data="16"/>
<high data="24"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="以晴为主"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周五"/>
<low data="15"/>
<high data="27"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="晴"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周六"/>
<low data="16"/>
<high data="28"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="晴"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周日"/>
<low data="17"/>
<high data="28"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="晴"/>
</forecast_conditions>
</weather>
</xml_api_reply>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值