2011-02-16 57 views
0

我有一個XML文件,我需要解析。這裏是(剝離爲清楚起見):如何使用NSXMLParser訪問完全相同的元素

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

的問題是,你可以看到,有兩個yweather:forecast元素,都沒有可用於在兩者之間進行區分的任何靜態文本。有任何想法嗎?

+0

我知道有類似的問題,但在方面略有不同。 – 2011-02-16 15:51:09

+1

你能告訴我們問題是什麼嗎?如果我解析這個,我可能會有一個代表「預測」節點的類並簡單地構建它們的一個數組。爲了幫助你,我們需要知道什麼是不工作的。 – MystikSpiral 2011-02-16 15:54:57

回答

0

啊,最後很容易。以下是我所做的:

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"]; 
    } 
} 

小菜一碟,對嗎? :)