I know it can be done by getElementbyId(tableId), but I don't have id attribute here. The table is like
<table>
<tr>
<td>data</td>
</tr>
<tr>
<td>data5</td>
</tr>
<tr>
<td>data1</td>
<td>data2</td>
</tr>
</table>
I am using Htmlunit. Is there any way to get the <td>
using htmlunit or java or any some api , JavaScript will work or not in I am not sure.
I know it can be done by getElementbyId(tableId), but I don't have id attribute here. The table is like
<table>
<tr>
<td>data</td>
</tr>
<tr>
<td>data5</td>
</tr>
<tr>
<td>data1</td>
<td>data2</td>
</tr>
</table>
I am using Htmlunit. Is there any way to get the <td>
using htmlunit or java or any some api , JavaScript will work or not in I am not sure.
- Your html code is bad, can you clean it up. – epascarello Commented Jan 10, 2012 at 6:26
- HTMLUnit can use xpath to reference elements. Read the docs. – epascarello Commented Jan 10, 2012 at 6:30
4 Answers
Reset to default 4You can do this in JavaScript using getElementsByTagName
.
Using javascript DOM traverse:
var table = document.getElementsByTagName("table")[0];
var tds = table.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
alert(tds[i].innerHTML);
}
Demo here: http://jsfiddle/AMbk7/
Jsoup will do everything you need wrt html parsing. Jsoup is a java api for handling html source code. You can get
- Table, with which you can parse each and every row or column.
- List of all the links and source imports to that html(imports like css and js files).
- Data of particular tag.
and more.
Hope this will help you.
Hey i am giving to you better way to find data in table.
First get list of HTML Table Rows.
then get list of HTML Table columns and use for loops and iterate table.
List<HtmlTableRow> tableRows = table.getRows();
List<HtmlTableCell> tableColumns = table.getRow(0).getCells();
for (int row = 0; row < tableRows.size(); row++)
{
for (int column = 0; column < tableColumns.size(); column++)
{
// do your work Here
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742312779a4420231.html
评论列表(0条)