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

发布于 2024-10-17 11:06:16 字数 726 浏览 3 评论 0原文

我有一个需要解析的 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 元素,并且两者都没有任何可用于区分二。有什么想法吗?

I have an XML file I'm needing to parse. Here it is (stripped for clarity):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-10-24 11:06:16

啊,结果很容易。这就是我所做的:

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

小菜一碟,对吧? :)

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? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文