从XML RSS获取属性

时间:2015-08-26 17:13:58

标签: xml vba

我有一个XML Feed我正试图从Excel中获取一些数据。这是XML

<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
<channel>
<title>Yahoo! Weather - Los Angeles, CA</title>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_f.html
</link>
<description>Yahoo! Weather for Los Angeles, CA</description>
<language>en-us</language>
<lastBuildDate>Wed, 26 Aug 2015 9:47 am PDT</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Los Angeles" region="CA" country="US"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="84" direction="0" speed="0"/>
<yweather:atmosphere humidity="55" visibility="7" pressure="29.97" rising="0"/>
<yweather:astronomy sunrise="6:20 am" sunset="7:25 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 Los Angeles, CA at 9:47 am PDT</title>
<geo:lat>34.05</geo:lat>
<geo:long>-118.23</geo:long>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_f.html
</link>
<pubDate>Wed, 26 Aug 2015 9:47 am PDT</pubDate>
<yweather:condition text="Fair" code="34" temp="84" date="Wed, 26 Aug 2015 9:47 am PDT"/>
<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 84 F<BR /> <BR /><b>Forecast:</b><BR /> Wed - Mostly Sunny. High: 89 Low: 71<br /> Thu - Sunny. High: 89 Low: 72<br /> Fri - Sunny. High: 92 Low: 71<br /> Sat - Mostly Sunny. High: 88 Low: 69<br /> Sun - Mostly Sunny. High: 82 Low: 66<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_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="Wed" date="26 Aug 2015" low="71" high="89" text="Mostly Sunny" code="34"/>
<yweather:forecast day="Thu" date="27 Aug 2015" low="72" high="89" text="Sunny" code="32"/>
<yweather:forecast day="Fri" date="28 Aug 2015" low="71" high="92" text="Sunny" code="32"/>
<yweather:forecast day="Sat" date="29 Aug 2015" low="69" high="88" text="Mostly Sunny" code="34"/>
<yweather:forecast day="Sun" date="30 Aug 2015" low="66" high="82" text="Mostly Sunny" code="34"/>
<guid isPermaLink="false">USCA0638_2015_08_30_7_00_PDT</guid>
</item>
</channel>
</rss>
<!--
 fan1591.sports.bf1.yahoo.com Wed Aug 26 10:09:26 PDT 2015 
-->

如何获取yweather:condition的元素,因为我正在尝试获取"temp"的值。或者,就此而言,如何获得< ... >之间的任何信息,如yweather:forecast的“高”,“低”等?

我知道如何获取更“纯粹”的XML文档的子元素 - 例如,this XML我能够使用以下内容解析信息:

Set resultnodes = oXMLFile.SelectNodes("/GeocodeResponse/result")
For Each n In resultnodes
            Set latitudenodes = n.SelectSingleNode("geometry/location/lat")
            Set LongitudeNodes = n.SelectSingleNode("geometry/location/lng")
            Set addressNodes = n.SelectSingleNode("formatted_address")
            Set countyNodes = n.SelectSingleNode("address_component[type='administrative_area_level_2']/long_name")

If Not latitudenodes Is Nothing Then latitude = latitudenodes.Text
            If Not LongitudeNodes Is Nothing Then longitude = LongitudeNodes.Text
            If Not addressNodes Is Nothing Then altAddress = addressNodes.Text

但使用类似的东西并不适用于雅虎。我认为/知道这是因为他们的XML布局不同 - 他们有文字和引号,而谷歌只是标签之间的值(“宾夕法尼亚大道SE”)。

我如何对雅虎做同样的事情? (如果它有不同的技术名称,那叫什么类型的XML?)。

感谢您的任何想法!

编辑:注意,我刚试过,我可以获取“”和“”值,因为它们遵循我用于Google的模式...它是{{1}内的值我不确定如何退出。

1 个答案:

答案 0 :(得分:1)

您尝试访问的节点以命名空间为前缀,因此您可能需要先定义该命名空间,然后才能访问该节点。此外,@运算符访问XPath查询中的属性。

以下内容应该可以从temp节点获取<yweather:condition>属性:

Dim doc
Set doc = CreateObject("MSXML2.DOMDocument.6.0")   
doc.Async = False
doc.Load "c:\path\to\your.xml"

' Specify the namespace being used (alias = "n1")...
doc.SetProperty "SelectionNamespaces", "xmlns:n1='http://xml.weather.yahoo.com/ns/rss/1.0'"

' Get the text of the "temp" attribute (note the "n1" namespace here)...
Debug.Print doc.SelectSingleNode("/rss/channel/item/n1:condition/@temp").Text

输出:

84

如果您想获得预测,可以使用selectNodes()获取节点列表,然后使用getAttribute()检索该特定节点的特定属性的值:

Dim n
For Each n In doc.selectNodes("/rss/channel/item/n1:forecast")
    Debug.Print n.getAttribute("day"), n.getAttribute("high")
Next

输出:

Wed           89
Thu           89
Fri           92
Sat           88
Sun           82

修改,关于评论:

要检索特定日期的单个值,您可以使用[@attr=value]语法查找符合条件的节点以及/@attr,以返回您感兴趣的属性。例如,在这里你可以如何抓住周三的高点:

Debug.Print doc.selectSingleNode("/rss/channel/item/n1:forecast[@day='Wed']/@high").Text