1
<table width="100%" border="0" cellpadding="0" cellspacing="1" class="table_border" id="center_table">
<tbody>
<tr>
<td width="25%" class="heading_table_top">S. No.</td>
<td width="45%" class="heading_table_top">
Booking Status (Coach No , Berth No., Quota)
</td>
<td width="30%" class="heading_table_top">
* Current Status (Coach No , Berth No.)
</td>
</tr>
</tbody>
</table>

I scrap a webpage and store the response in a string.

I then parse it into jsoup doc

Document doc = Jsoup.parse(result);

Then i select the table using

Element table=doc.select("table[id=center_table]").first();

Now i need to replace the text in tag "Booking Status (Coach No , Berth No., Quota)" to "Booking Status" using jsoup.. Could anybody help ?

I tried

  table.children().text().replaceAll(RegEx to select the text?????, "Booking Status");
ziaasp
  • 77
  • 2
  • 9

2 Answers2

3
        Elements tds=doc.select("table[id=center_table] td");  // select the tds from your table
        for(Element td : tds) {  // loop through them
            if(td.text().contains("Booking Status")) {  // found the one you want
                td.text("Booking Status");          // Replace with your text
            }
        }

then you can use doc.toString() to get the text of the HTML back to save to disk, send to a webView or whatever else you want to do with it.

iagreen
  • 31,470
  • 8
  • 76
  • 90
  • What do you mean form the table? If you mean get the HTML back, just do "String newHtml = doc.toString();", and you will have a string with the HTML in it. – iagreen Jan 14 '13 at 20:52
1
Elements tablecells=doc.select("table tbody tr td");

will give you 3 cells. use a loop to get the each element with

Element e=Elements.get(int index);

Use the e.text() to get the String.

Compare or replace strings with String.equals() , String.contains(), String.replace()

wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • Can you answer my question first, What do you mean by replace? What results are you looking for? – wtsang02 Jan 14 '13 at 17:58
  • I want to shorten the text "Booking Status (Coach No , Berth No., Quota)" to "Booking Status" . I dont want to extract the text to a string. I want to use Jsoup replace method to do this – ziaasp Jan 14 '13 at 18:05
  • And your html file is at web server or local disk? – wtsang02 Jan 14 '13 at 18:06
  • the Element "table" contains the html of the table.. I want to shorten the text before displaying the table in webview – ziaasp Jan 14 '13 at 18:06
  • the html is response from webrequest.. its temporarily stored in variable.. i am screen scrapping a website – ziaasp Jan 14 '13 at 18:07
  • Sorry I can't make it any more clear. If I do it more 'clear' Ill be writing your program for you. All the methods and classes I already stated in my answer. – wtsang02 Jan 14 '13 at 18:55