HtmlCleaner Version : 2.1
import java.io.IOException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
public class Test {
private String refactory(String htmlSource) {
HtmlCleaner cleaner = new HtmlCleaner();
// 設定清除選項
CleanerProperties p = cleaner.getProperties();
p.setOmitComments(false); // 不清除註解(有bug, 造成結構與原本不同)
p.setPruneTags("style,script"); // 去掉style, script標籤
TagNode node = null;
try {
node = cleaner.clean(htmlSource);
} catch (IOException e) {
e.printStackTrace();
}
// 重整結構
return cleaner.getInnerHtml(node);
}
}