Java – how to convert HTML to 2D arrays
•
Java
Let me say I copy a complete HTML table (when each TR and TD has additional attributes)
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:
PS: I know I can use regular expressions, but it will be very complex I want a tool like jsoup that can automatically do all the work without writing too much code
Solution
This is done using jsoup (srsly, don't use regexp for 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
}
In addition, in your example, the class property value is not closed correctly here:
<td class="bold>Td2</td>
It should be
<td class="bold">Td2</td>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
