views:

547

answers:

2

I'm trying to parse a Stack Overflow RSS feed of a specific question: http://stackoverflow.com/feeds/question/2110875

For this I'm using the TouchXML library. There seems to be a problem in the following code:

CXMLDocument *parser = [[CXMLDocument alloc] initWithData:sourceData options:0 error:nil];
NSArray *allEntries = [parser nodesForXPath:@"//entry" error:nil];
NSLog(@"Found entries: %d",[allEntries count]); //Returns 0

The NSLog statement should return the count of all entries in the feed. In this case it should be '3', problem is that it returns 0.

I found that this piece of code does work:

CXMLDocument *preParser = [[CXMLDocument alloc] initWithData:sourceData options:0 error:nil];
NSString *sourceStringUTF8 = [preParser XMLString];
[preParser release];

CXMLDocument *parser = [[CXMLDocument alloc] initWithData:[sourceStringUTF8 dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSArray *allEntries = [parser nodesForXPath:@"//entry" error:nil];
NSLog(@"Found entries: %d",[allEntries count]); //Returns 3, which is ok

But using this seems hacky (it probably is) and introduces a few other sporadic bugs.

As far as I know the Xpath expression is correct. I've checked it using this page as well.

Can anyone help me with this problem, or point me in the right direction.

Thanks.

A: 

Maybe start by actually using that error argument to nodesForXPath:error to see if it returns an error? And check if allEntries is not nil after making that call?

St3fan
I've logged an error message and it's null, so there is no error. The problem is that TouchXML simply doesn't return any Nodes for the Xpath statement, while AFAIK it should just find them.
Rengers
+1  A: 

I had a very similar problem. This has something to do with the xml namespace, which TouchXML doesn't support very well (a known issue).

I believe that in your hack, the namespace wasn't passed into the second parser, that's why it works.

A easier way is just to change

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

replaced with simply

<html>

and xPath now works.

ivanTheTerrible