NekoHTMLの多分よくない使い方

たとえばこんなHTMLがありまして。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head profile="http://selenium-ide.openqa.org/profiles/test-case">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="selenium.base" href="http://www.google.com/ig?hl=ja" />
        <title>test</title>
    </head>
    <body>
        <table cellpadding="1" cellspacing="1" border="1">
            <thead>
                <tr><td rowspan="1" colspan="3">test</td></tr>
            </thead>
            <tbody>
                <tr>
                    <td>open</td>
                    <td>/ig?hl=ja</td>
                    <td></td>
                </tr>
                <tr>
                    <td>type</td>
                    <td>q</td>
                    <td>cfneo</td>
                </tr>
                <tr>
                    <td>click</td>
                    <td>btnG</td>
                    <td></td>
                </tr>
                <tr>
                    <td>clickAndWait</td>
                    <td>btnG</td>
                    <td></td>
                </tr>
                <tr>
                    <td>clickAndWait</td>
                    <td>link=cfneoとは - はてなキーワード</td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

えぇ、SeleniumIDEに吐かせたテストケースです。これのTRタグの中だけ拾い読みするような、つまるトコロCSV的に取り扱うようにするにはどうするのかな、と。で、watijの持つ依存ライブラリの中にnekohtml-0.9.5.jarなんてものがあったので、NekoHTMLに初挑戦。

というか、今のところ完全な業務システム畑の人間なので、そもそもHTMLやらXMLをパースするって事が無かったなぁと思いつつ、こんな風にしてみました。

public class NekoHtmlTest {

    String path = "src/test/resources/selenium_test.html";

    @Test
    public void test() {
        try {
            DOMParser parser = new DOMParser();
            parser.parse(new InputSource(
                    new InputStreamReader(new FileInputStream(path), "UTF-8")));
            Document doc = parser.getDocument();
            Element root = doc.getDocumentElement();
            this.proc1(root);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    public void proc1(Element root) {
        if (root.getTagName().equals("TBODY")) {
            this.proc2(root, 0);
        } else {
            NodeList items = root.getChildNodes();
            for (int i = 0; i < items.getLength(); i++) {
                if (items.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    this.proc1((Element) items.item(i));
                }
            }
        }
    }

    public void proc2(Element root, int indent) {
        if ("TR".equals(root.getTagName())) {
            this.proc3((Element) root);
        } else {
            NodeList items = root.getChildNodes();
            for (int i = 0; i < items.getLength(); i++) {
                Node item = items.item(i);
                if (item.getNodeType() == Node.ELEMENT_NODE) {
                    this.proc2((Element) item, indent + 1);
                }
            }
        }
    }

    public void proc3(Element root) {
        NodeList items = root.getChildNodes();
        StringBuilder sb = new StringBuilder();
        int idx = 0;
        for (int i = 0; i < items.getLength(); i++) {
            Node item = items.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                Element e2 = (Element) item;
                if ("TD".equals(e2.getTagName()) && e2.hasChildNodes()) {
                    String s = e2.getFirstChild().getNodeValue().trim();
                    sb.append(s);
                    sb.append(",");
                    idx++;
                }
            }
        }
        // CSVで出力
        System.out.println(sb);
    }
}


なんか、もっと良いやり方があるだろう。
っていうか、proc3って何だよーorz


と思うのでありました><