使用 TouchXML 解析 SO RSS 提要,没有找到标签

发布于 2024-08-20 12:24:07 字数 1223 浏览 13 评论 0原文

我正在尝试解析 Stack Overflow RSS 提要的特定问题: https://stackoverflow.com/feeds/question/2110875

为此,我使用 TouchXML 库。以下代码似乎有问题:

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

NSLog 语句应返回提要中所有条目的计数。在这种情况下,它应该是“3”,问题是它返回 0。

我发现这段代码确实有效:

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

但是使用它似乎很hacky(它可能是)并且引入了一些其他零星的错误。

据我所知 Xpath 表达式是正确的。我也使用页面进行了检查。

任何人都可以帮助我解决这个问题,或者指出我正确的方向。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-08-27 12:24:07

我有一个非常相似的问题。这与 xml 命名空间有关,TouchXML 对此支持不是很好(一个已知问题)。

我相信在你的黑客中,名称空间没有传递到第二个解析器中,这就是它起作用的原因。

一种更简单的方法是只需将其替换

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

为 simple

<html>

,xPath 现在就可以工作了。

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.

只想待在家 2024-08-27 12:24:07

也许首先实际使用 nodesForXPath:errorerror 参数来查看它是否返回错误?并在调用后检查 allEntries 是否不是 nil

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文