我刚刚学习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 
  -->

解决方案 »

  1.   

    我的程序为:
    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怎么写?
      

  2.   

    foo.getChild("yweather:location").getAttribute("city").getValue()
      

  3.   

    dom4j中好象没有getChild().在哪个包下面呀
      

  4.   

    dom4j用foo.selectSingleNode("yweather:location").valueOf("@city")
      

  5.   

    for ( Iterator i = root.elementIterator("channel"); i.hasNext();) {
    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/>");
    还是不行?
      

  6.   

    我试过可以的,你先看看out.print(foo.selectSingleNode("yweather:location").asXML());
    看看是什么?贴出来看看
      

  7.   

    你把root,foo挨个print出来看看问题出在那一步
      

  8.   

    就是
    out.print("城市:"+foo.selectSingleNode("yweather:location").valueOf("@city")+"<br/>");

    out.print(foo.selectSingleNode("yweather:location").asXML());
    存在问题.
    我加了有有问题,
    不加显示:
    最后更新时间:Tue, 13 Dec 2005 11:00 am CST
      

  9.   

    用xpath这样试试!
             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());
             }
      

  10.   


          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");