0

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]です。

私は何を取りこぼしたか?

4

1 に答える 1

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]);
于 2012-08-27T21:59:23.963 に答える