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

时间:2021-12-02 01:09:06

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

我正在尝试解析一个特定问题的堆栈溢出RSS提要:https://*.com/feeds/question/2110875。

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

为此,我使用了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

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.

NSLog语句应该返回提要中所有条目的计数。这里应该是3,问题是返回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.

但是使用这种方法似乎很简单(很可能是这样),并引入了一些其他的零星bug。

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

就我所知,Xpath表达式是正确的。我也用这个页面检查过了。

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

有人能帮我解决这个问题吗?

Thanks.

谢谢。

2 个解决方案

#1


2  

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).

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

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

我相信在您的hack中,名称空间没有被传递到第二个解析器中,这就是它工作的原因。

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.

和xPath现在工作。

#2


0  

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?

也许可以从实际使用这个错误参数到nodesForXPath:看看它是否返回一个错误?然后在调用之后检查所有条目是否为nil ?

#1


2  

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).

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

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

我相信在您的hack中,名称空间没有被传递到第二个解析器中,这就是它工作的原因。

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.

和xPath现在工作。

#2


0  

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?

也许可以从实际使用这个错误参数到nodesForXPath:看看它是否返回一个错误?然后在调用之后检查所有条目是否为nil ?