Read XML

Discussion in 'Mac Programming' started by fernandovalente, Feb 20, 2010.

  1. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #1
    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. :)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What is being returned to you in the error variable?
     
  3. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #3
    The problem is that the array is not getting any value. I mean, the array is still empty.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #5
    Code:
    XQueryError:3 - "parse error invalid token ()- channel/sy:updateFrequency/item/" at line:1
    This is the error. Any help is appreciated.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     
  7. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #7
    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. :(
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    Did you ever get this working? If not, did you try "/rss/channel/title"? (assuming the root element is named "rss")
     
  9. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #9
    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.
     

Share This Page