RaptureXML无法获取子标签

时间:2012-08-27 21:40:55

标签: iphone ios xcode ipad ios5

我正在使用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]为零。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

我还没有使用过RaptureXML,但看起来你错过了一层。 rootXML可能是weatherdata,因此它没有forecast个孩子,因为它的唯一孩子是weather,其中有forecast个孩子。尝试添加:

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]);