Sam B
Sam B

Reputation: 27598

RaptureXML cannot get to children tags

I am using raptureXML to extract data from my XML <forecast .../> tags.

This is the 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>

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

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.

what did I miss?

Upvotes: 0

Views: 806

Answers (1)

Justin Paulson
Justin Paulson

Reputation: 4388

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

Then use weather for the rest of the code instead of rootXML.

Like so:

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

Upvotes: 2

Related Questions