<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>
我抓取一个网页并将响应存储在一个字符串中。
然后我将它解析成 jsoup 文档
Document doc = Jsoup.parse(result);
然后我选择表格使用
Element table=doc.select("table[id=center_table]").first();
现在我需要使用 jsoup 将标签“预订状态(车厢号、泊位号、配额)”中的文本替换为“预订状态”。有人可以帮忙吗?
我试过了
table.children().text().replaceAll(RegEx to select the text?????, "Booking Status");
最佳答案
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
}
}
然后您可以使用 doc.toString()
将 HTML 文本取回以保存到磁盘、发送到 webView 或您想要对其执行的任何其他操作。
关于java - 使用 Jsoup 替换标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14323443/