如何使用NSXMLParser访问完全相同的元素

时间:2011-02-16 15:50:41

标签: iphone xml nsxmlparser

我有一个我需要解析的XML文件。这是(为了清楚起见而剥离):

<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元素,并且两者都没有任何可用于区分两者的静态文本。有任何想法吗?

1 个答案:

答案 0 :(得分: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"];
    }
}

一块蛋糕,对吧? :)