问题描述
我正在使用raptureXML从XML <forecast .../>
标记中提取数据.
I am using raptureXML to extract data from my XML <forecast .../>
tags.
这是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
I use this code for 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]
为零时我什么也没得到.
Pretty straight forward right? but the problem is that it finds NO forecast tags i.e. I get nothing for the NSLog(@"high: %@", high);
and [forecast count]
is Zero.
我想念什么?
推荐答案
我没有使用过RaptureXML,但好像您错过了一层. rootXML
可能是weatherdata
,因此它没有forecast
子级,因为它的唯一子级是weather
,它确实具有forecast
子级.尝试添加:
I haven't used RaptureXML, but it looks like you missed a layer. rootXML
is probably the weatherdata
so it has no forecast
children, because its only child is weather
which does have forecast
children. Try adding:
RXMLElement *weather = [rootXML child:@"weather"];
然后在其余的代码中使用weather
而不是rootXML
.
Then use weather
for the rest of the code instead of 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]);
这篇关于RaptureXML无法到达子标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!