利 用xml,解析yahoo天气代码~~~ 我很菜,曾为这个天气代码搞了好几天,不过现在我终于可以实现了 网站首页上每天都能够有最新的天气情况,而不用自己手动搜集,确实方便了很多,下面我就将我的经验介绍一下 关于天气服务,我是用的yahoo提供的天气服务,网上搜索的时候,据说weather.com也提供这个服务,不过需要注册,我去看看了,甚至连注册的地方都没找到(汉自己的e文阿),就懒得用他们家的了 yahoo的天气服务地址是 http://xml.weather.yahoo.com/ 在yahoo的技术支持里对于天气的代码有详细的解释 http://developer.yahoo.com/weather/ 简单说来,我们可以发送请求到yahoo,它就会返回一个xml文件,请求的地址如下: http://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=c 我们得到的xml文件格式如下:
Yahoo! Weather - Beijing, CH >http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html
Yahoo! Weather for Beijing, CH
en-us
Tue, 26 Dec 2006 12:00 pm CST
60
Yahoo! Weather
142
18
>http://weather.yahoo.com/
http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif
Conditions for Beijing, CH at 12:00 pm CST
39.93
116.28
>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html
Tue, 26 Dec 2006 12:00 pm CST
Current Conditions:
Fair, 0 C
Forecast:
Tue - Sunny. High: 1 Low: -8
Wed - Sunny. High: 1 Low: -9
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]> CHXX0008_2006_12_26_12_0_CST 看到了吧,里面有我们需要的数据,这时我们只要通过xml操作读取到里面的数据就可以了。 xml的操作我所知道有dom,sax api和jdom三种方式,随便哪一种你都可以,我用了sax这种方式 Weather weather=new Weather(); Vector vector=new Vector(); YahooHandler yh=new YahooHandler(); URL url=new URL("http://localhost:8080/daemon/weather.xml"); InputStream input=url.openStream(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); SAXParser parser = factory.newSAXParser(); parser.parse(input, yh); vector=yh.getVector(); weather=(Weather)vector.elementAt(0); 在这里我有一个weather.class封装了一些weather的属性,一个yahoohandler用来解析数据 主要就是这么一些语句: public void startElement(String name,AttributeList attributes) throws SAXException{ String temp_date; if(name.equalsIgnoreCase("item")){ weather=new Weather(); }else if(name.equalsIgnoreCase("yweather:condition")){ temp_date=attributes.getValue("date"); System.out.println("pubDate is :"+temp_date); //weather.setPubDate(); }else if(name.equalsIgnoreCase("description")){ //System.out.println("When ther is description the temp_date is :"+temp_date); }else if(name.equalsIgnoreCase("yweather:forecast")){ temp_date=attributes.getValue("date"); String day=attributes.getValue("day"); int low=Integer.parseInt(attributes.getValue("low")); int high=Integer.parseInt(attributes.getValue("high")); String text=attributes.getValue("text"); //将字符串日期转换为日期格式 if(temp_sign==0){ weather.setTodayDay(day); weather.setTodayLow(low); weather.setTodayHigh(high); weather.setTodayText(text); //System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text); temp_sign=1; }else if(temp_sign==1){ weather.setTomDay(day); weather.setTomLow(low); weather.setTomHigh(high); weather.setTomText(text); //System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text); temp_sign=0; } }
Current Conditions:
Fair, 0 C
Forecast:
Tue - Sunny. High: 1 Low: -8
Wed - Sunny. High: 1 Low: -9
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]> CHXX0008_2006_12_26_12_0_CST 看到了吧,里面有我们需要的数据,这时我们只要通过xml操作读取到里面的数据就可以了。 xml的操作我所知道有dom,sax api和jdom三种方式,随便哪一种你都可以,我用了sax这种方式 Weather weather=new Weather(); Vector vector=new Vector(); YahooHandler yh=new YahooHandler(); URL url=new URL("http://localhost:8080/daemon/weather.xml"); InputStream input=url.openStream(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); SAXParser parser = factory.newSAXParser(); parser.parse(input, yh); vector=yh.getVector(); weather=(Weather)vector.elementAt(0); 在这里我有一个weather.class封装了一些weather的属性,一个yahoohandler用来解析数据 主要就是这么一些语句: public void startElement(String name,AttributeList attributes) throws SAXException{ String temp_date; if(name.equalsIgnoreCase("item")){ weather=new Weather(); }else if(name.equalsIgnoreCase("yweather:condition")){ temp_date=attributes.getValue("date"); System.out.println("pubDate is :"+temp_date); //weather.setPubDate(); }else if(name.equalsIgnoreCase("description")){ //System.out.println("When ther is description the temp_date is :"+temp_date); }else if(name.equalsIgnoreCase("yweather:forecast")){ temp_date=attributes.getValue("date"); String day=attributes.getValue("day"); int low=Integer.parseInt(attributes.getValue("low")); int high=Integer.parseInt(attributes.getValue("high")); String text=attributes.getValue("text"); //将字符串日期转换为日期格式 if(temp_sign==0){ weather.setTodayDay(day); weather.setTodayLow(low); weather.setTodayHigh(high); weather.setTodayText(text); //System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text); temp_sign=1; }else if(temp_sign==1){ weather.setTomDay(day); weather.setTomLow(low); weather.setTomHigh(high); weather.setTomText(text); //System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text); temp_sign=0; } }