iOS HTML解析问题

时间:2013-04-09 21:34:58

标签: html ios parsing tags html-parsing

我已将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,那么我需要检查否则没有标记“notification-item notification-item-error”,所以请告诉我另一个视图。

就我而言,在if之后,由于某种原因,我的代码将转到else,因此我的代码认为if条件为false并转到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

1 个答案:

答案 0 :(得分: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];
    }
}