Tuesday, 15 July 2014

Parsing HTML files with JSOUP

Often times we need to obtain data from sources on internet, or it might just be the case that you have a some data that you wish to extract from an HTML file. So in such scenario you could write your own parser, which would be not very efficient and may be buggy or you could a existing library such as JSOUP to do the job. In my particular scenario, I had a HTML table like this one

______________________________________________________________________________

Table 1

A B C 100
D E F 200
G H I 300

Table 2

A1 B1 C1 100
D1 E1 F1 200
G1 H1 I1 300

______________________________________________________________________________

 My code for parsing these HTML tables is as follows:

______________________________________________________________________________

package thundering.typhoons.example;

import java.io.File;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) {

        String pathToHtmlFile = "test.htm";

        try {
            parse(pathToHtmlFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void parse(String pathToHtmlFile) throws IOException {
        Document doc;

        File input = new File(pathToHtmlFile);

        doc = Jsoup.parse(input, "UTF-8");

        Elements heads = doc.select("h1");
        for (Element head : heads) {
            System.out.println("Contains header : " + head.text());
        }

        Elements tables = doc.select("table");
        for (Element table : tables) {

            System.out.println("Reading table : " + table.text());
            int elemInTable = table.select("td").size();
            int tableRows = table.select("tr").size();
            int numberofCols = (elemInTable / tableRows);

            System.out.println("Number of rows " + tableRows + ", number of columns : " + numberofCols);

            int j;
            for (j = 0; j < (elemInTable / numberofCols); j++) {
                String[] cols = new String[numberofCols];
                int k;
                for (k = 0; k < numberofCols; k++) {
                    cols[k] = table.select("td").get(numberofCols * j + k)
                            .text();
                    System.out.print(cols[k]+" ");

                }
                System.out.println();
            }
        }
    }
}

No comments:

Post a Comment