0

I'm reading a html file using jsoup. I want to show the html table,how can I do that?

I'm a beginner with jsoup - and a not that experienced java developer. :)

public class test {

    public static void main(String[] args) throws IOException {
        // TODO 自動產生的方法 Stub
        File input = new File("D://index.html");//從一個html文件讀取
        Document doc = Jsoup.parse(input,"UTF-8");

        //test
        Elements trs = doc.select("table").select("tr");

        for(Element e : trs) {
            System.out.println("-------------------");
            System.out.println(e.text());
        }
    }
}
soorapadman
  • 4,451
  • 7
  • 35
  • 47
許鴻章
  • 1
  • 1

1 Answers1

0

Without knowing jsoup, I guess you should descend into the html structure step by step, like this:

...
//test
Elements tables = doc.select("table");

for (Element table : tables) {
    for (Element row : table.select("tr")) {
        for (Element e : row.select("td")) {
            // output your td-contents here
            System.out.println("-------------------");
            System.out.println(e.text());
        }
    }
}
...

The advantage of this approach is that you have more control over drawing separators between the HTML Elements.

dirkeleid
  • 41
  • 2
  • 8