iOS HTML解析问题

问题描述:

我已经在我的项目中实现了HTMLParser。我连接到计费网站到我的手机运营商,我需要解析不同的变量。iOS HTML解析问题

HTML内容

我需要得到一个标记,它给我的指示,如果用户名和密码是不正常,并显示一个警告用户。

NSArray *spanNodes = [bodyNode findChildTags:@"div"]; 

for (HTMLNode *spanNode in spanNodes) { 
    if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"notification-item notification-item-error"]) { 
     //Above my tag 
     [self alertMessageLogin]; 
     NSLog(@"%@", [spanNode rawContents]); //Answer to second question 
    } 
    else { 
     NSlog(@"You are not logged in"); 
    } 
} 

但是,如果我再次加入到这个代码,所以我需要检查其他任何标记的“通知项目通知项错误”,所以给我另一种看法。

在我的情况下,if后,我的代码是由某种原因要else,所以我的代码认为if条件是假,要到else条件。但没有else所有工作正常。这是我的日志从else。我不知道,为什么它会打印几个“您已登录”?

如何解决此问题?

2013-04-09 23:48:50.828 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.831 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.831 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.831 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.832 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.832 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.832 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.834 GolanTelecom[90143:c07] You are logged in 
2013-04-09 23:48:50.834 GolanTelecom[90143:c07] <div 
class="notification-item notification-item-error"><div 
class="notification-item-inner"><span 
class="notification-content">???? ???? ?? ????? 
??????</span></div></div> 2013-04-09 23:48:50.834 
GolanTelecom[90143:c07] You are logged in 2013-04-09 23:48:50.835 
GolanTelecom[90143:c07] You are logged in 2013-04-09 23:48:50.835 
GolanTelecom[90143:c07] You are logged in 2013-04-09 23:48:50.835 
GolanTelecom[90143:c07] You are logged in 
+0

用'else'分支中的代码会有所帮助。 – 2013-04-09 22:42:50

+0

Hi A-Live我已经添加了其他例子 – Anton 2013-04-10 05:30:07

+0

我既不能NSLog登录'你已经登录'或者没有登录'你没有登录'。很难理解不完整的代码有什么问题。您可能想要尝试记录(或观看)'[spanNode getAttributeNamed:@“class”]'的值。 – 2013-04-10 08:04:30

这里是工作代码:

NSError *error = nil; 
    NSString *html = str; 
    HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error]; 

    if (error) { 
     NSLog(@"Error: %@", error); 
     return; 
    } 

    HTMLNode *bodyNode = [parser body]; 
    NSArray *spanNodes = [bodyNode findChildTags:@"div"]; 
    BOOL found = NO; 

    for (HTMLNode *spanNode in spanNodes) { 
     if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"notification-item notification-item-error"]) { 
      [self alertMessageLogin]; 
      found = YES; 
      NSLog(@"%@", [spanNode rawContents]); 
      break; 
     } 
    } 

    if (NO == found) { 
     //NSLog(@"Hey, I'm here!"); 
     [self HideLoginScreenWhenLogged]; 
    } 
}