HtmlCleaner是一个免费开源的适用范围广的Java语言Html文档解析器,它能重新整理HTML文档的每个元素并生成结构良好(Well-Formed)的 HTML 文档。默认它遵循的规则是类似于大部份web浏览器为创文档对象模型所使用的规则,户可以提供自定义tag和规则组来进行过滤和匹配。
HtmlCleaner软件特色
它被设计的小,快速,灵活而且独立。HtmlCleaner也可用在Java代码中,当命令行工具或Ant任务。 解析后编程轻量级文档对象,能够很容易的被转换到DOM或者JDom标准文档,或者通过各种方式(压缩,打印)连续输出XML。
HtmlCleaner使用示例
写一个测试用的html文件:html-clean-demo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = "http://www.w3.org/1999/xhtml " xml:lang = "zh-CN" dir = "ltr" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=GBK" />
< meta http-equiv = "Content-Language" content = "zh-CN" />
< title > html clean demo </ title >
</ head >
< body >
< div class = "d_1" >
< ul >
< li > bar </ li >
< li > foo </ li >
< li > gzz </ li >
</ ul >
</ div >
< div >
< ul >
< li > < a name = "my_href" href = "1.html" > text-1 </ a > </ li >
< li > < a name = "my_href" href = "2.html" > text-2 </ a > </ li >
< li > < a name = "my_href" href = "3.html" > text-3 </ a > </ li >
< li > < a name = "my_href" href = "4.html" > text-4 </ a > </ li >
</ ul >
</ div >
</ body >
</ html >
Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<meta http-equiv="Content-Language" content="zh-CN"/>
<title>html clean demo</title>
</head>
<body>
<div class="d_1">
<ul>
<li>bar</li>
<li>foo</li>
<li>gzz</li>
</ul>
</div>
<div>
<ul>
<li><a name="my_href" href="1.html">text-1</a></li>
<li><a name="my_href" href="2.html">text-2</a></li>
<li><a name="my_href" href="3.html">text-3</a></li>
<li><a name="my_href" href="4.html">text-4</a></li>
</ul>
</div>
</body>
</html>
模拟需求:取出title,name="my_href" 的链接,div的class="d_1"下的所有li内容。下面用htmlcleaner写代码,HtmlCleanerDemo.java
package com.chenlb;
import java.io.File;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
/**
* htmlcleaner 使用示例.
*
* @author chenlb 2008-11-26 下午02:12:02
*/
public class HtmlCleanerDemo {
public static void main(String[] args) throws Exception {
HtmlCleaner cleaner = new HtmlCleaner();
TagNode node = cleaner.clean(new File( "html/html-clean-demo.html" ), "GBK" );
//按tag取.
Object[] ns = node.getElementsByName("title" , true ); //标题
if (ns.length > 0 ) {
System.out.println("title=" +((TagNode)ns[ 0 ]).getText());
}
System.out.println("ul/li:" );
//按xpath取
ns = node.evaluateXPath("//div[@class='d_1']//li" );
for (Object on : ns) {
TagNode n = (TagNode) on;
System.out.println("\ttext=" +n.getText());
}
System.out.println("a:" );
//按属性值取
ns = node.getElementsByAttValue("name" , "my_href" , true , true );
for (Object on : ns) {
TagNode n = (TagNode) on;
System.out.println("\thref=" +n.getAttributeByName( "href" )+ ", text=" +n.getText());
}
}
}
Java代码
package com.chenlb;
import java.io.File;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
/**
* htmlcleaner 使用示例.
*
* @author chenlb 2008-11-26 下午02:12:02
*/
public class HtmlCleanerDemo {
public static void main(String[] args) throws Exception {
HtmlCleaner cleaner = new HtmlCleaner();
TagNode node = cleaner.clean(new File("html/html-clean-demo.html"), "GBK");
//按tag取.
Object[] ns = node.getElementsByName("title", true); //标题
if(ns.length > 0) {
System.out.println("title="+((TagNode)ns[0]).getText());
}
System.out.println("ul/li:");
//按xpath取
ns = node.evaluateXPath("//div[@class='d_1']//li");
for(Object on : ns) {
TagNode n = (TagNode) on;
System.out.println("\ttext="+n.getText());
}
System.out.println("a:");
//按属性值取
ns = node.getElementsByAttValue("name", "my_href", true, true);
for(Object on : ns) {
TagNode n = (TagNode) on;
System.out.println("\thref="+n.getAttributeByName("href")+", text="+n.getText());
}
}
}
cleaner.clean()中的参数,可以是文件,可以是url,可以是字符串内容。个人认为:比较常用的应该是evaluateXPath、 getElementsByAttValue、getElementsByName方法了。另外说明下,htmlcleaner 对不规范的html兼容性比较好。
HtmlCleaner更新内容
1.HtmlCleaner的文档对象模型拥有了一些函数,处理节点和属性,所以在序列化之前搜索或者编辑是非常容易的。
2.提供基本HtmlCleaner DOM的XPath支持
3.使用XML配置文件让创建定制tag变得更加容易
4.修复多个bug以及API改进
- PC官方版
- 安卓官方手机版
- IOS官方手机版