HtmlCleaner是一個(gè)免費(fèi)開源的適用范圍廣的Java語言Html文檔解析器,它能重新整理HTML文檔的每個(gè)元素并生成結(jié)構(gòu)良好(Well-Formed)的 HTML 文檔。默認(rèn)它遵循的規(guī)則是類似于大部份web瀏覽器為創(chuàng)文檔對(duì)象模型所使用的規(guī)則,戶可以提供自定義tag和規(guī)則組來進(jìn)行過濾和匹配。
HtmlCleaner軟件特色
它被設(shè)計(jì)的小,快速,靈活而且獨(dú)立。HtmlCleaner也可用在Java代碼中,當(dāng)命令行工具或Ant任務(wù)。 解析后編程輕量級(jí)文檔對(duì)象,能夠很容易的被轉(zhuǎn)換到DOM或者JDom標(biāo)準(zhǔn)文檔,或者通過各種方式(壓縮,打印)連續(xù)輸出XML。
HtmlCleaner使用示例
寫一個(gè)測(cè)試用的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"下的所有l(wèi)i內(nèi)容。下面用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 ); //標(biāo)題
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); //標(biāo)題
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()中的參數(shù),可以是文件,可以是url,可以是字符串內(nèi)容。個(gè)人認(rèn)為:比較常用的應(yīng)該是evaluateXPath、 getElementsByAttValue、getElementsByName方法了。另外說明下,htmlcleaner 對(duì)不規(guī)范的html兼容性比較好。
HtmlCleaner更新內(nèi)容
1.HtmlCleaner的文檔對(duì)象模型擁有了一些函數(shù),處理節(jié)點(diǎn)和屬性,所以在序列化之前搜索或者編輯是非常容易的。
2.提供基本HtmlCleaner DOM的XPath支持
3.使用XML配置文件讓創(chuàng)建定制tag變得更加容易
4.修復(fù)多個(gè)bug以及API改進(jìn)
- PC官方版
- 安卓官方手機(jī)版
- IOS官方手機(jī)版