How to access exact same elements using NSXMLParser
I have an XML file I'm needing to parse. Here it is (stripped for c开发者_Go百科larity):
<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>
<item>
<title>Yahoo! Weather - Somecity</title>
<yweather:astronomy sunrise="6:52 am" sunset="5:36 pm"/>
<yweather:forecast day="Wed" date="16 Feb 2011" low="39" high="59" text="Mostly Sunny" code="34"/>
<yweather:forecast day="Thu" date="17 Feb 2011" low="29" high="50" text="Mostly Sunny" code="34"/>
</item>
</channel>
</rss>
The problem is, as you can see, there are two yweather:forecast
elements, and both don't have any static text that can be used to differentiate between the two. Any ideas?
Ah, ended up being quite easy. Here's what I did:
if([elementName isEqualToString:@"yweather:forecast"]) {
if (counter == 0) {
TodaysHigh = [attributeDict objectForKey:@"high"];
TodaysLow = [attributeDict objectForKey:@"low"];
counter ++; //where counter is an instance variable
}
if (counter == 1) {
TomorrowsLow = [attributeDict objectForKey:@"low"];
TomorrowsHigh = [attributeDict objectForKey:@"high"];
TomorrowsCondition = [attributeDict objectForKey:@"text"];
TomorrowsConditionCode = [attributeDict objectForKey:@"code"];
}
}
Piece of cake, right? :)
精彩评论