How to convert HTML <table> to a 2D array


Let's say I copy the full HTML table (where each trand tdhave additional attributes) in a row. How can I take all the content (which is between the tags) and create a 2D array that is organized as a source table?

For example, for this table:

<table border="1">
    <tr align= "center">
        <td align="char">TD1</td>
        <td>td1</td>
        <td align="char">TD1</td>
        <td>td1</td>
    </tr>
    <tr>
        <td>TD2</td>
        <td>tD2</td>
        <td class="bold>Td2</td>
        <td>td2</td>
    </tr>
</table>

I want this array: array

PS: I know I can use regex, but that would be extremely difficult. I want a tool like JSoup to be able to do all the work automatically without significant code writing.

+3
source share
4 answers

JSoup (srsly, regexp HTML).

Document doc = Jsoup.parse(html);
Elements tables = doc.select("table");
for (Element table : tables) {
    Elements trs = table.select("tr");
    String[][] trtd = new String[trs.size()][];
    for (int i = 0; i < trs.size(); i++) {
        Elements tds = trs.get(i).select("td");
        trtd[i] = new String[tds.size()];
        for (int j = 0; j < tds.size(); j++) {
            trtd[i][j] = tds.get(j).text(); 
        }
    }
    // trtd now contains the desired array for this table
}

, class :

<td class="bold>Td2</td>

<td class="bold">Td2</td>
+8

, String.split('<whateverhtmltabletag>') ?

StringTokenizer. :

String data = "one<br>two<br>three";  
StringTokenizer tokens = new StringTokenizer(data, "<br>");  
while (tokens.hasMoreElements()) {  
   System.out.println(tokens.nextElement());  // prints one, then two, then three
}

, indexOf("<tag"), : http://forums.devshed.com/java-help-9/parse-html-table-into-2d-arrays-680614.html

HTML ( jsoup), . javascript: JavaScript HTML

+4

Nevermind, : HtmlTableParser

Actually, it seems that now I have another problem, but this is not entirely related to this issue, so I will open another one.

0
source

that I'm still not the best, but I hope this is useful ... just with a line

public void read_data() {
    try {
        file = new File("_result.xml");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = "";
        String output = "";
        int a = 0, b = 0;
        boolean _write = false;

        while ((line = bufferedReader.readLine()) != null) {
            if(line.trim().startsWith("<td")) { _write = true; } else { _write = false; }

            if(_write) {
                a = line.indexOf('>')+1;
                b = line.lastIndexOf('<');
                output += line.substring(a,b) + "|";
            }

            if(line.trim().equals("</tr>")) {
                System.out.println(output);
                output = "";
            }

        }
        fileReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
0
source

Source: https://habr.com/ru/post/1681947/


All Articles