iPhone,使用 TouchXML 解析 HTML

发布于 2024-09-10 23:21:16 字数 744 浏览 8 评论 0 原文

你们中有人有在 iPhone 上使用 TouchXML 库解析 HTML 的经验吗?我想解析一些 html,因此尝试执行以下操作,

self.parser = [[CXMLDocument alloc]initWithData:self.html options:0 error:&error1];

 if (error1) {
  NSLog(@"Error: %d", error1);
 }

 NSError *error;

 NSArray *resultNodes = [[NSArray alloc]init];

 NSLog(@"starting to do some crazy parsing");


 resultNodes = [self.parser nodesForXPath:@"//div" error:&error];
 if (error)
  NSLog(@"initWithData error : %d", error);

不幸的是,这根本不起作用。而且我不知道如何正确调试它。我的 HTML 应该有效。它只是以 html 标签开头,这意味着没有文档类型。 initWithData 方法似乎已经崩溃并返回以下错误: 错误:错误域=CXMLErrorDomain代码=-1“操作无法完成。(CXMLErrorDomain错误-1。)”如果我尝试输出第二个错误,应用程序之前崩溃,可能是由于initWithData不起作用。

你们中有人有过使用 TouchXML 库解析 HTML 的经验吗?

感谢您的帮助!

has any of you some experience with parsing HTML with the TouchXML lib on the iPhone. I would like to parse some html and therefore try to do the following

self.parser = [[CXMLDocument alloc]initWithData:self.html options:0 error:&error1];

 if (error1) {
  NSLog(@"Error: %d", error1);
 }

 NSError *error;

 NSArray *resultNodes = [[NSArray alloc]init];

 NSLog(@"starting to do some crazy parsing");


 resultNodes = [self.parser nodesForXPath:@"//div" error:&error];
 if (error)
  NSLog(@"initWithData error : %d", error);

Unfortunately that does not work at all. And I dont know how to debug this properly. My HTML should be valid. It simply starts with a html tag, meaning there is no doctype. The initWithData method already seems to crash and returns the following error:
Error: Error Domain=CXMLErrorDomain Code=-1 "The operation couldn’t be completed. (CXMLErrorDomain error -1.)" If I try to output the second error the app crashes before, probably cause of the fact that initWithData does not work.

Has anyone of you made some experience with parsing HTML with the TouchXML lib?

Thanks for any help!

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

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

发布评论

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

评论(2

旧竹 2024-09-17 23:21:16

以下对我来说工作得非常好。尝试一下。

 CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
resultNodes = [rssParser nodesForXPath:@"//item" error:nil];

// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
  ....

还要确保您的链接正确。有几次当我使用 feed://www.foo.com/feed 而不是 http://www.foo.com/feed 时,这种情况发生在我身上/www.foo.com/feed

干杯。

Following have been working absolutely fine for me. Try it.

 CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
resultNodes = [rssParser nodesForXPath:@"//item" error:nil];

// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
  ....

Also make sure that your link is right. Couple of times it happened to me while I was using feed://www.foo.com/feed instead of http://www.foo.com/feed

Cheers.

望笑 2024-09-17 23:21:16

首先也是最重要的,error是一个指向对象的指针,所以如果你想把它打印出来。它应该是: NSLog(@"%@", error1); 错误相同。如果你想打印错误代码。您可以执行NSLog(@"%d", [error1 code])。你给我们的只是错误的内存区域

First and foremost, error is a pointer to an object, so if you want to print it out. It should be : NSLog(@"%@", error1); The same for error. If you want to print error code. You can do NSLog(@"%d", [error1 code]). What you give us is just the memory area of the error

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