简体   繁体   English

iPhone,使用TouchXML解析HTML

[英]iPhone, parsing HTML with TouchXML

has any of you some experience with parsing HTML with the TouchXML lib on the iPhone. 您对使用iPhone上的TouchXML库解析HTML有一定的经验。 I would like to parse some html and therefore try to do the following 我想解析一些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);

Unfortunately that does not work at all. 不幸的是,这根本不起作用。 And I dont know how to debug this properly. 而且我不知道如何正确调试它。 My HTML should be valid. 我的HTML应该是有效的。 It simply starts with a html tag, meaning there is no doctype. 它只是以html标记开头,这意味着没有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. initWithData方法似乎已经崩溃,并返回以下错误:错误:错误域= CXMLErrorDomain代码= -1“该操作无法完成。(CXMLErrorDomain错误-1。)”如果我尝试输出第二个错误,则该应用程序崩溃之前,可能是由于initWithData不起作用的事实。

Has anyone of you made some experience with parsing HTML with the TouchXML lib? 你们中的任何人是否有使用TouchXML库解析HTML的经验?

Thanks for any help! 谢谢你的帮助!

First and foremost, error is a pointer to an object, so if you want to print it out. 首先,错误是指向对象的指针,因此如果要打印出来,则为错误。 It should be : NSLog(@"%@", error1); 应该是: NSLog(@"%@", error1); The same for error. 错误也一样。 If you want to print error code. 如果要打印错误代码。 You can do NSLog(@"%d", [error1 code]) . 您可以执行NSLog(@"%d", [error1 code]) What you give us is just the memory area of the error 您给我们的只是错误的存储区域

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 在我使用feed://www.foo.com/feed而不是http://www.foo.com/feed时发生了几次

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM