Get attribute from XML RSS

匿名 (未验证) 提交于 2019-12-03 09:13:36

问题:

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  --> 

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?

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 

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

How can I do the same with the Yahoo one? (What's that type of XML called, if it has a different technical name?).

Thanks for any ideas!

Edit: 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.

回答1:

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.

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 

Output:

84 

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 

Output:

Wed           89 Thu           89 Fri           92 Sat           88 Sun           82 

Edit, with respect to comments:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!