【问题标题】:Replacing text inside tags using Jsoup使用 Jsoup 替换标签内的文本
【发布时间】:2013-01-14 17:26:12
【问题描述】:
<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 doc

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");

【问题讨论】:

  • 编辑了我的问题。请检查
  • 而且您还清楚地确定了您想要实现的目标? “替换文字”似乎很模糊

标签: java android html parsing jsoup


【解决方案1】:
        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 或您想要对其执行的任何其他操作。

【讨论】:

  • 表格表格是什么意思?如果您的意思是取回 HTML,只需执行“String newHtml = doc.toString();”,您就会得到一个包含 HTML 的字符串。
【解决方案2】:
Elements tablecells=doc.select("table tbody tr td");

会给你 3 个细胞。 使用循环来获取每个元素

Element e=Elements.get(int index);

使用e.text() 获取字符串。

String.equals() , String.contains(), String.replace()比较或替换字符串

【讨论】:

  • 你能先回答我的问题吗,替换是什么意思?您在寻找什么结果?
  • 我想将文本“预订状态(教练号,泊位号,配额)”缩短为“预订状态”。我不想将文本提取到字符串中。我想使用 Jsoup 替换方法来做到这一点
  • 你的html文件是在web服务器还是本地磁盘?
  • 元素“table”包含表格的html。我想在webview中显示表格之前缩短文本
  • html 是来自 webrequest 的响应。它临时存储在变量中。我正在屏幕报废一个网站
猜你喜欢
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 2013-05-22
  • 2013-08-23
  • 2014-08-13
  • 1970-01-01
相关资源
最近更新 更多