开发者

Parsing SO RSS feed using TouchXML, no <entry> tags found

I'm trying to parse a Stack Overflow RSS feed of a specific question: https://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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜