2011-02-16 61 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"]; 
    } 
} 

小菜一碟,对吗? :)