admin管理员组文章数量:1332873
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
}
}
本文标签:
版权声明:本文标题:javascript - How to iterate over table and geting data within <td> tag without using id attribute of tabel in java 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312779a2451208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论