XML访问元素

时间:2014-04-21 05:57:30

标签: xml groovy

我一直在尝试使用Groovy中的XMLSlurper访问XML中的特定元素。这是我的XML文件。

<?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 - San Jose, CA</title>
        <link>http://us.rd.yahoo.com/dailynews/rss/weather/San_Jose__CA/*http://weather.yahoo.com/forecast/USCA0993_f.html</link>
        <description>Yahoo! Weather for San Jose, CA</description>
        <language>en-us</language>
        <lastBuildDate>Sun, 20 Apr 2014 9:53 pm PDT</lastBuildDate>
        <ttl>60</ttl>
        <yweather:location city="San Jose" region="CA"   country="US"/>
        <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
        <yweather:wind chill="61"   direction="290"   speed="8" />
        <yweather:atmosphere humidity="52"  visibility="10"  pressure="30.1"  rising="1" />
        <yweather:astronomy sunrise="6:25 am"   sunset="7:47 pm"/>
        <image>
            <title>Yahoo! Weather</title>
            <width>142</width>
            <height>18</height>
            <link>http://weather.yahoo.com</link>
            <url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
        </image>
        <item>
            <title>Conditions for San Jose, CA at 9:53 pm PDT</title>
            <geo:lat>37.34</geo:lat>
            <geo:long>-121.89</geo:long>
            <link>http://us.rd.yahoo.com/dailynews/rss/weather/San_Jose__CA/*http://weather.yahoo.com/forecast/USCA0993_f.html</link>
            <pubDate>Sun, 20 Apr 2014 9:53 pm PDT</pubDate>
            <yweather:condition  text="Fair"  code="33"  temp="61"  date="Sun, 20 Apr 2014 9:53 pm PDT" />
            <description><![CDATA[
                <img src="http://l.yimg.com/a/i/us/we/52/33.gif"/><br />
                <b>Current Conditions:</b><br />
                Fair, 61 F<BR />
                <BR /><b>Forecast:</b><BR />
                Sun - Mostly Clear. High: 79 Low: 51<br />
                Mon - Mostly Cloudy. High: 73 Low: 51<br />
                Tue - Partly Cloudy. High: 65 Low: 46<br />
                Wed - Partly Cloudy. High: 69 Low: 52<br />
                Thu - Mostly Sunny. High: 69 Low: 53<br />
                <br />
                <a href="http://us.rd.yahoo.com/dailynews/rss/weather/San_Jose__CA/*http://weather.yahoo.com/forecast/USCA0993_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
                (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
            ]]></description>
            <yweather:forecast day="Sun" date="20 Apr 2014" low="51" high="79" text="Mostly Clear" code="33" />
            <yweather:forecast day="Mon" date="21 Apr 2014" low="51" high="73" text="Mostly Cloudy" code="28" />
            <yweather:forecast day="Tue" date="22 Apr 2014" low="46" high="65" text="Partly Cloudy" code="30" />
            <yweather:forecast day="Wed" date="23 Apr 2014" low="52" high="69" text="Partly Cloudy" code="30" />
            <yweather:forecast day="Thu" date="24 Apr 2014" low="53" high="69" text="Mostly Sunny" code="34" />
            <guid isPermaLink="false">USCA0993_2014_04_24_7_00_PDT</guid>
        </item>
    </channel>
</rss>

这是我要访问的常规代码。

 def rss = new XmlSlurper().parseText(xml)
 println "\t" + rss.channel.item.condition.@date

一切都很完美。唯一的问题是我无法访问xml代码最后几行中出现的其他日期的预测。 <yweather:forecast ....>

我试过了:

println rss.channel.item.forecast.@day

我输出为SunMonTueWedThu

我需要逐个访问它的其他部分。 我确信我做错了什么。谁能帮我吗?

谢谢,

2 个答案:

答案 0 :(得分:1)

rss.channel.item.forecast.collect{ it.@day }应该为您提供天数列表而不是串联字符串。

如果要访问节点的其他属性,可以这样做:rss.channel.item.forecast.collect{ "${it.@day} - low:${it.@low} - high:${it.@high}"}

答案 1 :(得分:1)

我稍后想出来了。认为它也可能对其他人有用..还有另一种方法来处理这个问题,

println rss.channel.item.forecast.@day[0]

这将让您直接访问特定属性!