5 Star 5 Fork 1

Aron / YTTHpplePlus

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or Download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

YTTHpplePlus 是Hpple的扩展,在Hpple的基础上扩展了 HTML/XML文档 节点的增删改查文档导出功能,原始的Hpple库可以从 Hpple在GitHub上的链接 中查看,感谢作者原始的贡献。

  • 安装
pod 'YTTHpplePlus'

#####对应功能的使用方法参考单元测试类NewPageTest中找到,支持的功能如下:

  • 更新或者添加属性
- (void)testSetAttr {
    NSArray *imgs = [self.doc searchWithXPathQuery:@"//img"];
    
    for (TFHppleElement* element in imgs) {
        NSString* raw = element.raw;
        NSString* tagName = element.tagName;
        NSString* content = element.content;
        NSDictionary* attributes = element.attributes;
        
        NSString* src = [element objectForKey:@"src"];
        
    }
    
    // 设置第一个元素的属性
    if (imgs.count > 0) {
        TFHppleElement* element = imgs.firstObject;
        [self.doc setOrUpdateAttribute:@{@"width": @"13131"} inElement:element];
    }
    
    [self.doc exportXmlDoc];
    
    NSLog(@"=");
}
  • 更新节点的内容,比如<p>,<a>,<div>等标签中的内容
- (void)testSetContent {
    NSArray *paragraphes = [self.doc searchWithXPathQuery:@"//p"];
    if (paragraphes.count) {
        TFHppleElement* element = paragraphes.firstObject;
        [self.doc setOrUpdateContent:@"这个是更新替换后的内容" inElement:element];
    }
    
    [self.doc exportXmlDoc];
}
  • 删除节点,子接点也会对应的从文档中删除
- (void)testRemoveNode {
    NSArray *paragraphes = [self.doc searchWithXPathQuery:@"//p"];
    if (paragraphes.count) {
        TFHppleElement* element = paragraphes.firstObject;
        [self.doc deleteElement:element];
    }
    
    [self.doc exportXmlDoc];
}
  • 导出文档,导出增删改操作之后的文档
// 增删改查操作
NSString* exportedHtmlStr = [self.doc exportXmlDoc];

#####TODOS

  • 文档导出会导致有部分双标签变成了单标签,比如<script>标签,这导致了web浏览器加载HTML文档出问题,目前项目中我使用了两种方法解决这个问题,希望有人如果有更好的方案,可以分享一下。
  1. 正则替换script标签
            NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"<script.*</script>" options:0 error:nil];
            content  = [regularExpression stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:@""];
  1. script标签中添加一些额外的内容,然后最终生成文档的时候再进行替换
<script src="../js/news-script.js">var abcd;</script>
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<![CDATA[var abcd;]]>" withString:@""];

Repository Comments ( 0 )

Sign in to post a comment

About

功能强大的HTML/XML文档的解析、节点的增删改查、修改结果的导出 expand collapse
Objective-C
MIT
Cancel

Releases

No release

YTTHpplePlus

Contributors

All

Activities

Load More
can not load any more
Objective-C
1
https://git.oschina.net/dhar/YTTHpplePlus.git
git@git.oschina.net:dhar/YTTHpplePlus.git
dhar
YTTHpplePlus
YTTHpplePlus
master

Search