【问题标题】:iOS HTML parse issueiOS HTML解析问题
【发布时间】:2013-04-01 12:51:22
【问题描述】:

我已经在我的项目中实现了 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 no tag "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

【问题讨论】:

  • 带有else 分支的代码会有帮助。
  • 嗨 A-Live 我在示例中添加了其他内容
  • 我既不能为You are logged in 使用NSLog,也不能记录You are not logged in 的消息。不完整的代码将很难理解有什么问题。您可能想尝试记录(或查看)[spanNode getAttributeNamed:@"class"] 的值。
  • 这里是颠倒的。如果此“标签”未记录,则记录。我会检查你的建议。
  • @A-Live 这个日志给我看this tag 2013-04-10 14:48:44.932 GolanTelecom[98621:c07] notification-item notification-item-error。我认为因为它是循环,所以它试图在所有内容中查找这个标签,然后如果它没有找到任何东西,它会返回错误并转到其他条件。但是我如何在没有循环的情况下在所有内容中搜索我的标签?我认为这是一个问题。或者我需要我不知道找到我的标签时如何停止这个循环

标签: html ios parsing tags html-parsing


【解决方案1】:

这是工作代码:

    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];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多