0

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

来自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");
    }
}

但是如果我在这段代码中添加了 else ,那么我需要检查 else 没有标签“notification-item notification-item-error”,所以给我看另一个视图。

在我的情况下,之后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
4

1 回答 1

0

这是工作代码:

    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];
    }
}
于 2013-04-10T15:00:35.737 回答