Re: How to iterate the html table content using webspec API

189 views
Skip to first unread message

Lance

unread,
Aug 17, 2012, 12:34:10 PM8/17/12
to wa...@googlegroups.com
Here below is a table data structure for extracting HTML content.  You use it by finding your header row and other rows and add them iteratively:

        Tag table = spec.find.table().with().title("<Your Table Name Here>");
      
        Tag body = table.child("tbody");
        Tag rows = body.child.tr();
        Table queue = new Table();
        queue.addSchema(rows.at(0));
        for(int row = 1; row <rows.all().length(); row++) {
            queue.add(rows.at(row));
        }

To insert a row you will have to do that with javascript or jquery.  This should give you an idea of where to start if you have java-script experience.

public class Table {
    HashMap schemaLookup = new HashMap();
    String[] schema;
    ArrayList<String[]> rows = new ArrayList(0);
   
    void addSchema(Tag tr) {
        Tag tableHeader = tr.child.th();
        schema = new String[tableHeader.all.length()];
//        schema = new String[tr.child.th().all.length()];
        for(int column = 0; column < tableHeader.all.length(); column++) {
            schemaLookup.put(tableHeader.at(column).get.innerHTML(), column);
            schema[column] = tableHeader.at(column).get.innerHTML();
        }
    }
   
    void add(Tag tr) {
        String[] row = new String[schema.length];
        for(int column = 0; column < tr.child.td().all().length(); column++) {
            Tag tableCell = tr.child.td(column).child("nobr");
            row[column] = tableCell.get().innerHTML();
        }
        rows.add(row);
    }
   
    void print() {
        System.out.println(Arrays.toString(schema));
        for(int row = 0; row < rows.size(); row++) {
            System.out.println(Arrays.toString(rows.get(row)));
        }
    }



On Wednesday, August 1, 2012 1:19:33 AM UTC-5, veeraja wrote:
Hi ,

I am new to the Webspec API.Could you please help me regarding the iterating the HTML table content.Actually my requirement is to insert the new row  to the table .

Thanks in Advance,
Veeraja

Reply all
Reply to author
Forward
0 new messages