gpt4 book ai didi

iphone - RaptureXML 无法访问子标签

转载 作者:行者123 更新时间:2023-11-28 22:50:16 24 4
gpt4 key购买 nike

我正在使用 raptureXML 从我的 XML 中提取数据 <forecast .../>标签。

这是 XML

<?xml version="1.0" ?>
<weatherdata>
<weather weatherlocationname="Chicago, IL">

<forecast low="67" high="86" skycodeday="34" skytextday="Mostly Sunny" date="2012-08-27" day="Monday" shortday="Mon" precip="0" />

<forecast low="66" high="82" skycodeday="34" skytextday="Mostly Sunny" date="2012-08-28" day="Tuesday" shortday="Tue" precip="0" />

<forecast low="66" high="82" skycodeday="34" skytextday="Mostly Sunny" date="2012-08-29" day="Wednesday" shortday="Wed" precip="0" />

<forecast low="68" high="88" skycodeday="32" skytextday="Sunny (Clear)" date="2012-08-30" day="Thursday" shortday="Thu" precip="0" />

<forecast low="70" high="90" skycodeday="34" skytextday="Mostly Sunny" date="2012-08-31" day="Friday" shortday="Fri" precip="0" />

<toolbar timewindow="60" minversion="1.0.1965.0" />
</weather>
</weatherdata>

我将此代码用于 raptureXML

RXMLElement *rootXML = [RXMLElement elementFromXMLString:XML encoding:NSUTF8StringEncoding];

[rootXML iterate:@"forecast" usingBlock:^(RXMLElement *element)
{
NSString *high = [element attribute:@"low"];
NSLog(@"high: %@", high);
}];

NSArray *forecast = [rootXML children:@"forecast"];
NSLog(@"[forecast count]: %d", [forecast count]);

很简单吧?但问题是它没有找到预测标签,即我对 NSLog(@"high: %@", high); 一无所获和 [forecast count]是零。

我错过了什么?

最佳答案

我没有使用过 RaptureXML,但看起来您漏掉了一层。 rootXML 可能是 weatherdata 所以它没有 forecast child ,因为它唯一的 child 是 weatherforecast child 。尝试添加:

RXMLElement *weather = [rootXML child:@"weather"];

然后在其余代码中使用 weather 而不是 rootXML

像这样:

RXMLElement *rootXML = [RXMLElement elementFromXMLString:XML encoding:NSUTF8StringEncoding];

RXMLElement *weather = [rootXML child:@"weather"];

[weather iterate:@"forecast" usingBlock:^(RXMLElement *element)
{
NSString *high = [element attribute:@"low"];
NSLog(@"high: %@", high);
}];

NSArray *forecast = [weather children:@"forecast"];
NSLog(@"[forecast count]: %d", [forecast count]);

关于iphone - RaptureXML 无法访问子标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12149987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com