前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS中HTML的解析——Hpple

iOS中HTML的解析——Hpple

作者头像
Oceanlong
发布2018-07-03 13:19:56
2.1K0
发布2018-07-03 13:19:56
举报

前言

iOS中,当我们需要解析xmlhtml时,我们可以使用libxml2来进行解析。但由于libxml2的api设计比较繁琐,使用起来并不方便。Hpple则是基于libxml2的oc库,使 我们可以用其方便地进行xmlhtml的解析。

使用方法

我们先来看看,我们需要解析的是什么样的对象。

代码语言:javascript
复制
NSString *htmlString  = @"<p style='color:blue;font-size:16px;'>Hell<font color='red'>o w</font>orld</p>";

这是一段普通的html,设置了一段文字的字体大小和颜色。我可以先看展示效果。

image.png

我们在解析这一段html时,希望得到的,是它的标签名,内容和属性。接下来看一下Hpple是如何帮我们完成这些的:

代码语言:javascript
复制
    NSString *htmlString  = @"<p style='color:blue;font-size:16px;'>Hell<font color='red'>o w</font>orld</p>";
    // 将html字符串转为NSData
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    // 创建Hpple对象
    xpathParser = [[TFHpple alloc] initWithHTMLData:data];
    // 搜索XPath寻找标签
    NSArray *elements  = [xpathParser searchWithXPathQuery:@"//p"];
    // Output
    TFHppleElement *element = [elements objectAtIndex:0];
    NSLog(@"content:%@" , [element content]);
    NSLog(@"tagName:%@",[element tagName]);
    NSLog(@"attributes:%@",[element attributes]);    

我们搜索文本中的<p>获得如下输出:

代码语言:javascript
复制
2018-03-03 19:29:01.249456+0800 HppleDemo[8877:1175700] content:Hello world
2018-03-03 19:29:01.249579+0800 HppleDemo[8877:1175700] tagName:p
2018-03-03 19:29:01.249743+0800 HppleDemo[8877:1175700] attributes:{
    style = "color:red;font-size:16px;";
}

这些输出中,我们获得了<p>的标签名、内容和style。但我们没有获取到<font>的信息。 所幸Hpple为我们提供了hasChildrenchildren两个方法,我们可以以此来获得子标签的属性:

代码语言:javascript
复制
    NSString *htmlString  = @"<p style='color:blue;font-size:16px;'>Hell<font color='red'>o w</font>orld</p>";
    // 将html字符串转为NSData
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    // 创建Hpple对象
    xpathParser = [[TFHpple alloc] initWithHTMLData:data];
    // 搜索XPath寻找标签
    NSArray *elements  = [xpathParser searchWithXPathQuery:@"//p"];
    // Output
    TFHppleElement *element = [elements objectAtIndex:0];
    [self logElemen:element];
    if ([element hasChildren]) {
        NSArray* children = [element children];
        for(int i = 0 ; i < [children count] ; i++){
            TFHppleElement *subElement = children[i];
            [self logElemen:subElement];
        }
    }

- (void) logElemen:(TFHppleElement*) element{
    NSLog(@"raw:%@",[element raw]);
    NSLog(@"content:%@" , [element content]);
    NSLog(@"tagName:%@",[element tagName]);
    NSLog(@"attributes:%@",[element attributes]);
}

这样修改之后,我们可以看到<p>的子标签也被一一打印了出来:

代码语言:javascript
复制
2018-03-03 20:32:27.724729+0800 HppleDemo[9647:1226741] raw:<p style="color:blue;font-size:16px;">Hell<font color="red">o w</font>orld</p>
2018-03-03 20:32:27.724873+0800 HppleDemo[9647:1226741] content:Hello world
2018-03-03 20:32:27.724982+0800 HppleDemo[9647:1226741] tagName:p
2018-03-03 20:32:27.725157+0800 HppleDemo[9647:1226741] attributes:{
    style = "color:blue;font-size:16px;";
}
2018-03-03 20:32:27.725267+0800 HppleDemo[9647:1226741] raw:(null)
2018-03-03 20:32:27.725361+0800 HppleDemo[9647:1226741] content:Hell
2018-03-03 20:32:27.725464+0800 HppleDemo[9647:1226741] tagName:text
2018-03-03 20:32:27.725553+0800 HppleDemo[9647:1226741] attributes:{
}
2018-03-03 20:32:27.726085+0800 HppleDemo[9647:1226741] raw:<font color="red">o w</font>
2018-03-03 20:32:27.726243+0800 HppleDemo[9647:1226741] content:o w
2018-03-03 20:32:27.726401+0800 HppleDemo[9647:1226741] tagName:font
2018-03-03 20:32:27.726551+0800 HppleDemo[9647:1226741] attributes:{
    color = red;
}
2018-03-03 20:32:27.726694+0800 HppleDemo[9647:1226741] raw:(null)
2018-03-03 20:32:27.739766+0800 HppleDemo[9647:1226741] content:orld
2018-03-03 20:32:27.739906+0800 HppleDemo[9647:1226741] tagName:text
2018-03-03 20:32:27.740002+0800 HppleDemo[9647:1226741] attributes:{
}

以上,就是Hpple的基本使用,如有问题,欢迎指正。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 使用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档