URLを指定してHTMLを読み込みドキュメントツリーを見るテスト
package jp.seraphyware.htmlparsertest;
import java.net.URL;
import java.net.URLConnection;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.util.NodeList;
public class HTMLParserTest {
public static void main(final String[] args) throws Exception {
final URL url = new URL("http://java.sun.com/");
final URLConnection urlConnection = url.openConnection();
final Parser parser = new Parser(urlConnection);
final NodeList lst = parser.parse(null);
walkTree("", lst);
}
private static void walkTree(final String level,
final NodeList lst) throws Exception {
if (lst == null) {
return;
}
final int cnt = lst.size();
for (int idx = 0; idx < cnt; idx++) {
final Node node = lst.elementAt(idx);
if (node instanceof Tag) {
final String tagName = ((Tag) node).getTagName();
System.out.println(level + "<" + tagName + ">");
walkTree(level + " ", node.getChildren());
}
else if (node instanceof Text) {
final String txt = node.getText();
if (txt.trim().length() > 0) {
System.out.println(level + txt);
}
}
}
}
}
所感
簡単だしコンパクトにかけそうだが、JAXP互換ではないし、そもそもメソッド名、クラス名も違うので、このあたりはJAVADOCをみながらアタリをつけてゆかなければならないぽい。
使った感じでは特に難しい点はなさそう。
NekoHTMLよりもいいかも。