vzm
vzm

Reputation: 2450

Error while working with nodesForXPath

In my app, I am fetching radio station information from XML.

To demonstrate my problem I will use two radio stations: One called "Christmas Classics" and one called "Christmas Power".

When I use the following code to get JUST christmas classics and load it into my tableView it works just fine:

DDXMLNode* rootNode = [xmlDoc rootElement];
    NSArray* nodes = nil;
    nodes = [rootNode nodesForXPath:@"child::Station[Name='Christmas Classics']" error:nil];
    for(DDXMLNode* node in nodes)
    {
        Station* s = [[Station alloc] init];
        DDXMLNode* nameNode = [[node nodesForXPath:@"Name" error:nil] objectAtIndex:0];
        s.name = [nameNode stringValue];
        DDXMLNode* bitRateNode = [[node nodesForXPath:@"Streams/Stream/BitRate" error:nil] objectAtIndex:0];
        s.bitRate = [bitRateNode stringValue];
        DDXMLNode* streamNode = [[node nodesForXPath:@"Streams/Stream/StreamUrl" error:nil] objectAtIndex:0];
        s.streamURL = [streamNode stringValue];
        [array addObject:s];
    }

However, when I attempt to get "Christmas Power" in the same fashion (both at the same time) by implementing:

 DDXMLNode* rootNode = [xmlDoc rootElement];
    NSArray* nodes = nil;
    nodes = [rootNode nodesForXPath:@"child::Station[Name='Christmas Classics']" error:nil];
    for(DDXMLNode* node in nodes)
    {
        Station* s = [[Station alloc] init];
        DDXMLNode* nameNode = [[node nodesForXPath:@"Name" error:nil] objectAtIndex:0];
        s.name = [nameNode stringValue];
        DDXMLNode* bitRateNode = [[node nodesForXPath:@"Streams/Stream/BitRate" error:nil] objectAtIndex:0];
        s.bitRate = [bitRateNode stringValue];
        DDXMLNode* streamNode = [[node nodesForXPath:@"Streams/Stream/StreamUrl" error:nil] objectAtIndex:0];
        s.streamURL = [streamNode stringValue];
        [array addObject:s];
    }

    NSArray *node2 = nil;
    node2 = [rootNode nodesForXPath:@"child::Station[Name='Christmas Power']" error:nil];
    for(DDXMLNode* node in nodes)
    {
        Station* b = [[Station alloc] init];
        DDXMLNode* nameNode = [[node nodesForXPath:@"Name" error:nil] objectAtIndex:0];
        b.name = [nameNode stringValue];
        DDXMLNode* bitRateNode = [[node nodesForXPath:@"Streams/Stream/BitRate" error:nil] objectAtIndex:0];
        b.bitRate = [bitRateNode stringValue];
        DDXMLNode* streamNode = [[node nodesForXPath:@"Streams/Stream/StreamUrl" error:nil] objectAtIndex:0];
        b.streamURL = [streamNode stringValue];
        [array addObject:b];
    }

This is the result that I get:

enter image description here

My table view loads up, but instead of having a listing for Christmas Classics and a listing for Christmas Power, I get 2 Christmas Classics listings.. Why is this happening? What Im I doing wrong? Is there a better way to go about this? All of the stations that I am working on are located in the same root location of /Station

I just need to be able to specify which ones to bring back, I don't want 100 listings returned, I just want the ones that I specify back....

Upvotes: 1

Views: 213

Answers (1)

Marcus Rickert
Marcus Rickert

Reputation: 4238

I think you have a tiny little a bug in your code. Although you have two different node sets nodes and node2 you iterate over the first node set in both cases:

for(DDXMLNode* node in nodes)

I suggest that you change the second iterator to

for(DDXMLNode* node in node2)

and see what happens.

Upvotes: 1

Related Questions