1 Star 0 Fork 1

webas / YTTHpplePlus

forked from Aron / YTTHpplePlus 
Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

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:@""];
Copyright (c) 2017 flypigrmvb <862709539@qq.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

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

Releases

No release

Contributors

All

Activities

Load More
can not load any more
Objective-C
1
https://gitee.com/yizhan5694/YTTHpplePlus.git
git@gitee.com:yizhan5694/YTTHpplePlus.git
yizhan5694
YTTHpplePlus
YTTHpplePlus
master

Search