GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Project Page
  • Releases
  • Project Page
  • Download
  • Getting Started
  • WebClient - The Browser
  • Content Extraction
  • Form Inputs
  • Tables
  • File Download
  • File Upload
  • Keyboard Simulation
  • Windows & Frames
  • JavaScript Support

This view

  • Original page
  • New address

GeezerVison is showing www.htmlunit.org — Table Tutorial – HtmlUnit

  • Using Tables
  • Complex Tables

Using Tables

The first set of examples uses this simple HTML table:

<html><head><title>Table sample</title></head><body>
    <table id="table1">
        <tr>
            <th>Number</th>
            <th>Description</th>
        </tr>
        <tr>
            <td>5</td>
            <td>Bicycle</td>
        </tr>
    </table>
</body></html>

This example demonstrates how to iterate over all rows and cells:

final HtmlTable table = page.getHtmlElementById("table1");
for (final HtmlTableRow row : table.getRows()) {
    System.out.println("Found row");
    for (final HtmlTableCell cell : row.getCells()) {
        System.out.println("   Found cell: " + cell.asNormalizedText());
    }
}

The following sample shows how to access specific cells by zero-based row and column indices:

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://foo.com");

final HtmlTable table = page.getHtmlElementById("table1");
// Access cell at row index 1 (second row) and column index 1 (second column)
final HtmlTableCell cell = table.getCellAt(1, 1);
System.out.println("Cell (1,1)=" + cell.asNormalizedText());

Complex Tables

The next examples use a more complex table containing header, footer, and body sections as well as a caption:

<html><head><title>Table sample</title></head><body>
    <table id="table1">
        <caption>My complex table</caption>
        <thead>
            <tr>
                <th>Number</th>
                <th>Description</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td>7</td>
                <td></td>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>5</td>
                <td>Bicycle</td>
            </tr>
        </tbody>
        <tbody>
            <tr>
                <td>2</td>
                <td>Tricycle</td>
            </tr>
        </tbody>
    </table>
</body></html>

HtmlTableHeader, HtmlTableFooter, and HtmlTableBody elements group rows together. A table can contain at most one header and one footer, but may contain multiple body sections. Each section provides access to its rows via getRows():

final HtmlTableHeader header = table.getHeader();
final List<HtmlTableRow> headerRows = header.getRows();

final HtmlTableFooter footer = table.getFooter();
final List<HtmlTableRow> footerRows = footer.getRows();

for (final HtmlTableBody body : table.getBodies()) {
    final List<HtmlTableRow> rows = body.getRows();
    // process rows
}

Tables may also optionally include a caption element describing their content:

final String caption = table.getCaptionText();
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.