从 XML RSS 获取属性 [英] Get attribute from XML RSS
问题描述
我有一个 XML 提要,我想从 Excel 中获取一些数据.这是XML:
I have an XML feed I'm trying to get some data from in Excel. Here's the 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
的高"、低"等?
How do I get the elements for the yweather:condition
, as I'm trying to get the value for "temp"
. Or, for that matter, how do I get any of the info between < ... >
, like yweather:forecast
's "high", "low", etc?
我知道如何获取更多纯"XML 文档的子元素 - 例如,这个 XML 我能够解析出以下信息:
I know how to get child elements of more "pure" XML documents - for example, this XML I am able to parse out info with the following:
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 的布局不同 - 他们有文本和引号,而谷歌只是带有 ("Pennsylvania Ave SE") 之间的值的标签.
But using something like that isn't working with the Yahoo one. I think/know it's because their XML is laid out differently - they have text and quotes, whereas the Google one is just the tags with the value between ("Pennsylvania Ave SE").
我怎样才能对雅虎做同样的事情?(这种类型的 XML 叫什么名字,如果它有不同的技术名称?).
How can I do the same with the Yahoo one? (What's that type of XML called, if it has a different technical name?).
感谢您的任何想法!
注意,我刚刚尝试过,我可以获得 "" 和 "" 值,因为它们遵循我与 Google 一起使用的模式……这是 中的值<... >
我不确定如何拉出来.
Note, I just tried and I can get the "" and "" values, since they follow my pattern I use with Google...it's the values that are inside < ... >
that I'm unsure how to pull out.
推荐答案
您尝试访问的节点以命名空间为前缀,因此您可能需要先定义该命名空间,然后才能访问该节点.此外,@
运算符访问 XPath 查询中的属性.
The node you're trying to access is prefixed with a namespace, so you may need to define that namespace before you can access the node. Also, the @
operator accesses an attribute within an XPath query.
以下应该可以从
节点获取 temp
属性:
The following should work to get the temp
attribute from the <yweather:condition>
node:
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()
来检索特定属性的值对于该特定节点:
If you want to get the forecast, you can use selectNodes()
to get a nodelist and and then use getAttribute()
to retrieve the value of a specific attribute for that particular node:
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
来查找与您的条件匹配的节点返回您感兴趣的属性.例如,您可以通过以下方式获取周三的最高价:
To retrieve a single value for a specific day, you can use the [@attr=value]
syntax to find the node matching your criteria along with /@attr
to return the attribute you're interested in. For example, here's how you can grab the high for Wednesday:
Debug.Print doc.selectSingleNode("/rss/channel/item/n1:forecast[@day='Wed']/@high").Text
这篇关于从 XML RSS 获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!