2
我試圖按列解析HTML表,我想我已經有了一般的算法。但rowpans正在給我帶來麻煩。按列解析HTML表
這是我使用的代碼:
Elements rows = document.select("table.asio_basic > tbody > tr"); // get all tablerows
Elements dataCells = new Elements(); //Object to save all cells with data
for (int i = 0; i < rows.get(0).children().size(); i++) //iterate through the columns.
{
for (int j = 0; j < rows.size(); j++) //iterate through the rows
{
Element cell = rows.get(j).child(i); //get the cell in row j, column i
if (cell.hasAttr("rowspan"))
{
j += Integer.parseInt(cell.attr("rowspan")); // add rowspan to counter to skip nonexistent cells
dataCells.add(cell);
}
}
}
所以我的問題是,小區的一排位置不符合其列對應,我已經後已經通過了一個rowpans列。
只是從單元格獲取所有數據不是一個選項,因爲我需要標題中的日期才能正確保存數據。
+1我一直有跨越的問題,除了我正在使用的表具有colspans和行跨度這是非常可怕的。我的方法與你的方法類似,因爲我試圖跟蹤生成的跨度,但在我的情況下,我一直在追蹤行/ colspans類似於由row/col索引的二維數組,因爲它保持爲單數是不夠的。 – MxyL
@MxyL是的,我一直在想這樣的事情,儘管我希望能有比這更優雅的東西。如果我能解決問題,我一定會在這裏發佈。 –