javascript - How to iterate over table and geting data within <td> tag without using id attribute of tabel in java

I know it can be done by getElementbyId(tableId), but I don't have id attribute here. The table is

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.

Share Improve this question edited Mar 26, 2012 at 19:35 animuson 54.8k28 gold badges142 silver badges150 bronze badges asked Jan 10, 2012 at 6:10 saum22saum22 88212 silver badges28 bronze badges 2
  • 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
Add a ment  | 

4 Answers 4

Reset to default 4

You 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

  1. Table, with which you can parse each and every row or column.
  2. List of all the links and source imports to that html(imports like css and js files).
  3. 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信