我刚刚学习xml,想利用资源做一个天气预报程序.下列是一个rss(xml文件),地址是http://xml.weather.yahoo.com/forecastrss?p=CHXX0120&u=c <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
- <channel>
<title>Yahoo! Weather - Shenzhen, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Shenzhen__CH/*http://xml.weather.yahoo.com/forecast/CHXX0120_c.html</link>
<description>Yahoo! Weather for Shenzhen, CH</description>
<language>en-us</language>
<lastBuildDate>Tue, 13 Dec 2005 10:00 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Shenzhen" region="" country="CH" />
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />
<yweather:wind chill="14" direction="30" speed="21" />
<yweather:atmosphere humidity="38" visibility="700" pressure="0" rising="0" />
<yweather:astronomy sunrise="6:55 am" sunset="5:41 pm" />
- <image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com/</link>
<url>http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
- <item>
<title>Conditions for Shenzhen, CH at 10:00 am CST</title>
<geo:lat>22.53</geo:lat>
<geo:long>114.13</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Shenzhen__CH/*http://xml.weather.yahoo.com/forecast/CHXX0120_c.html</link>
<pubDate>Tue, 13 Dec 2005 10:00 am CST</pubDate>
<yweather:condition text="Sunny" code="32" temp="14" date="Tue, 13 Dec 2005 10:00 am CST" />
- <description>
- <![CDATA[
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/32.gif" /><br />
<b>Current Conditions:</b><br />
Sunny, 14 C<p />
<b>Forecast:</b><BR />
Wed - Sunny. High: 17 Low: 10<br />
Thu - Sunny. High: 17 Low: 10<br />
Fri - Sunny. High: 18 Low: 10<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Shenzhen__CH/*http://xml.weather.yahoo.com/forecast/CHXX0120_c.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]>
</description>
<yweather:forecast day="Wed" date="13 Dec 2005" low="10" high="17" text="Sunny" code="32" />
<yweather:forecast day="Thu" date="14 Dec 2005" low="10" high="17" text="Sunny" code="32" />
<yweather:forecast day="Fri" date="15 Dec 2005" low="10" high="18" text="Sunny" code="32" />
<guid isPermaLink="false">CHXX0120_2005_12_13_10_0_CST</guid>
</item>
</channel>
</rss>
- <!-- p2.weather.scd.yahoo.com uncompressed/chunked Tue Dec 13 18:49:30 PST 2005
-->
- <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
- <channel>
<title>Yahoo! Weather - Shenzhen, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Shenzhen__CH/*http://xml.weather.yahoo.com/forecast/CHXX0120_c.html</link>
<description>Yahoo! Weather for Shenzhen, CH</description>
<language>en-us</language>
<lastBuildDate>Tue, 13 Dec 2005 10:00 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Shenzhen" region="" country="CH" />
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />
<yweather:wind chill="14" direction="30" speed="21" />
<yweather:atmosphere humidity="38" visibility="700" pressure="0" rising="0" />
<yweather:astronomy sunrise="6:55 am" sunset="5:41 pm" />
- <image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com/</link>
<url>http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
- <item>
<title>Conditions for Shenzhen, CH at 10:00 am CST</title>
<geo:lat>22.53</geo:lat>
<geo:long>114.13</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Shenzhen__CH/*http://xml.weather.yahoo.com/forecast/CHXX0120_c.html</link>
<pubDate>Tue, 13 Dec 2005 10:00 am CST</pubDate>
<yweather:condition text="Sunny" code="32" temp="14" date="Tue, 13 Dec 2005 10:00 am CST" />
- <description>
- <![CDATA[
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/32.gif" /><br />
<b>Current Conditions:</b><br />
Sunny, 14 C<p />
<b>Forecast:</b><BR />
Wed - Sunny. High: 17 Low: 10<br />
Thu - Sunny. High: 17 Low: 10<br />
Fri - Sunny. High: 18 Low: 10<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Shenzhen__CH/*http://xml.weather.yahoo.com/forecast/CHXX0120_c.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]>
</description>
<yweather:forecast day="Wed" date="13 Dec 2005" low="10" high="17" text="Sunny" code="32" />
<yweather:forecast day="Thu" date="14 Dec 2005" low="10" high="17" text="Sunny" code="32" />
<yweather:forecast day="Fri" date="15 Dec 2005" low="10" high="18" text="Sunny" code="32" />
<guid isPermaLink="false">CHXX0120_2005_12_13_10_0_CST</guid>
</item>
</channel>
</rss>
- <!-- p2.weather.scd.yahoo.com uncompressed/chunked Tue Dec 13 18:49:30 PST 2005
-->
解决方案 »
- Java实现单点登录(多域)
- 帮我看看这段java 代码, 提示 missing ( before formal parameters), 在线等, 马上给分
- 从JSP批量插入数据到ORACLE数据库插不进去,请指教!
- 有需要下载WebSphere的朋友吗?
- myeclipse6.5 webshpere6.1整合问题
- 有没有一个比较好点的编辑器啊
- 请教:gb2312字符集的问题?急!急!急!急!急!急!
- 输入框中要求输入0-100的整数,怎样避免输入字符,标点,小数...?
- Struts取值的问题!谢谢
- 请高手给我提供一个已经调试好了的jsp聊天室
- 急聘:Java软件工程师
- 【求助】使用DynaValidatorFrom出现ValidatorException,请达人指点
String url="";
url="http://xml.weather.yahoo.com/forecastrss?p=CHXX0120&u=c";
SAXReader reader = new SAXReader();
Document doc = reader.read(url);
Element root = doc.getRootElement();
for ( Iterator i = root.elementIterator("channel"); i.hasNext();) {
Element foo = (Element) i.next();
out.print("最后更新时间:"+foo.elementText("lastBuildDate")+"<br>");
}
这是ok的. 输出为:
最后更新时间:Tue, 13 Dec 2005 11:00 am CST
我想得到<yweather:location city="Shenzhen" region="" country="CH" />
中的ctiy的值Shenzhen怎么写?
Element foo = (Element) i.next();
out.print("最后更新时间:"+foo.elementText("lastBuildDate")+"<br/>");
out.print("城市:"+foo.selectSingleNode("yweather:location").valueOf("@city")+"<br/>");
}out.print("城市:"+foo.selectSingleNode("yweather:location").valueOf("@city")+"<br/>");
还是不行?
看看是什么?贴出来看看
out.print("城市:"+foo.selectSingleNode("yweather:location").valueOf("@city")+"<br/>");
或
out.print(foo.selectSingleNode("yweather:location").asXML());
存在问题.
我加了有有问题,
不加显示:
最后更新时间:Tue, 13 Dec 2005 11:00 am CST
Document doc = reader.read(yourFile);
//定位,第一个里面的city值
List node = doc.selectNodes( "//yweather:location[1]/@city" );
Iterator rs_linkId = node.iterator();
for (; rs_linkId.hasNext();) {
Attribute att = (Attribute)rs_linkId.next();
log.info(att.getValue());
}
Element channel=doc.getRootElement().element("channel"); System.out.print("标题:"+channel.elementText("title")+"\n");
System.out.print("描述:"+channel.elementText("description")+"\n");
System.out.print("时间:"+channel.elementText("lastBuildDate")+"\n");
System.out.print("ttl :"+channel.elementText("ttl")+"\n"); System.out.print("城市:"+channel.valueOf("yweather:location/@city")+
" "+channel.valueOf("yweather:location/@region")+
" "+channel.valueOf("yweather:location/@country")+"\n");