Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fernandovalente

macrumors 6502
Original poster
I'm trying to read a RSS from a WordPress blog. I wanna read the URL to the post, the post title, the post author and the date published. I guess the problem is on nodesForXPath:error:

Code:
itemNodes = [[doc nodesForXPath:@"channel/sy:updateFrequency" error:&error] retain];

I don't know what's wrong. Any help is appreciated. :)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The problem is that the array is not getting any value. I mean, the array is still empty.
I understand the problem perfectly. The reason for this may be in that an error is occurring. If this is the case then you should inspect the value pointed to by error. Otherwise there may, I suppose, simply be no matching nodes for the path provided.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Break it down. Does passing only "channel" as the path return anything? If not, try "/channel". "sy:" may also be the problem. That appears to be a namespace, I'm not sure if that is valid.
 

fernandovalente

macrumors 6502
Original poster
Break it down. Does passing only "channel" as the path return anything? If not, try "/channel". "sy:" may also be the problem. That appears to be a namespace, I'm not sure if that is valid.

Tried this now:

Code:
itemNodes = [[doc nodesForXPath:@"/channel" error:&error] retain];

Then tried this:
Code:
itemNodes = [[doc nodesForXPath:@"/channel/title" error:&error] retain];

I got no error on both, but the array is still empty. :(
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Did you ever get this working? If not, did you try "/rss/channel/title"? (assuming the root element is named "rss")
 

fernandovalente

macrumors 6502
Original poster
Did you ever get this working? If not, did you try "/rss/channel/title"? (assuming the root element is named "rss")

That's my code:

Code:
-(IBAction)refresh:(id)sender{
	[progress startAnimation:nil];
	NSURL *url = [NSURL URLWithString:@"http://www.macmaniacos.com/blog/feed/"];
	NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
	NSURLResponse *response;
	NSError *error;
	NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
	if(!urlData){
		NSAlert *a = [NSAlert alertWithError:error];
		[a runModal];
		return;
	}
	if(doc)
		[doc release];
	doc = [[NSXMLDocument alloc] initWithData:urlData options:0 error:&error];
	if(!doc){
		NSAlert *a = [NSAlert alertWithError:error];
		[a runModal];
		return;
	}
	if(itemNodes)
		[itemNodes release];
	itemNodes = [[doc nodesForXPath:@"/rss/channel/item/title" error:&error] retain];
	NSLog(@"%@", itemNodes);
	if(!itemNodes){
		NSAlert *a = [NSAlert alertWithError:error];
		[a runModal];
		return;
	}
	NSLog(@"%@", doc);
	[tableView reloadData];	
	[progress stopAnimation:nil];
}
-(NSString *)stringForPath:(NSString *)xp ofNode:(NSXMLNode *)n{
	NSError *error;
	NSArray *nodes = [n nodesForXPath:xp error:&error];
	if(!nodes){
		NSAlert *a = [NSAlert alertWithError:error];
		[a runModal];
		return nil;
	}
	if([nodes count] == 0)
		return nil;
	else{
		return [[nodes objectAtIndex:0] stringValue];
	}
}
-(int)numberOfRowsInTableView:(NSTableView *)tv{
	NSLog(@"%d", [itemNodes count]);
	return [itemNodes count];
}
-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)column row:(int)row{
	NSLog(@"%@", [self stringForPath:[column identifier] ofNode:[itemNodes objectAtIndex:row]]);
	return [self stringForPath:[column identifier] ofNode:[itemNodes objectAtIndex:row]];
}

With this, I could get the title of the articles, but when I try "/rss/channel/item" in order to get other things such as description and things like that, the array keeps empty. This must be a foolish mistake.

NOTE: As I'm still studing it, you can consider me a web services n00b.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.