java - 包含 a 的 div 中的 Jsoup 文本 "subtraction"

标签 java jsoup

使用 jsoup,我知道如何提取整个 div 的文本:

<div class="c">
<a href="/relurl.php?refid=7">First Anchor Text</a>
 Something in Between 
 <a href="/john.doe?refid=7">Second Anchor Text</a>
</div>

div.text() 产生:

First Anchor Text Something in Between Second Anchor Text

而且我知道如何分别提取每个 anchor 的文本,以便第一个 a.text() 产生:

First Anchor Text

但是在 Jsoup 中是否有一种优雅的方法来提取 Something Between

(我当然可以提取 2 个 a.text() 并从 div.text() 中“减去”它们,但我不认为这很优雅)

最佳答案

使用 Element#ownText() .这是链接的 javadoc 的摘录:

ownText

public String ownText()

Gets the text owned by this element only; does not get the combined text of all children.

For example, given HTML <p>Hello <b>there</b> now!</p>, p.ownText() returns "Hello now!", whereas p.text() returns "Hello there now!". Note that the text within the b element is not returned, as it is not a direct child of the p element.

所以,应该这样做:

String ownText = div.ownText();
// ...

关于java - 包含 a 的 div 中的 Jsoup 文本 "subtraction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6155217/

相关文章:

java - log4j 有接口(interface)/GUI appender 吗?

java - 为什么这个正则表达式不起作用?

java - Helloworld 数据库 - Android 与 sqlite 的连接

java - ADT/Eclipse 升级后库出现问题

java - 无法在 Java 中扩展类

java - 自定义适配器在 ArrayList 准备好之前启动

java - 如何将 Jsoup 添加到我的 Android Studio 项目中?

c# - Windows 应用商店应用程序的 NSoup 替代方案

java - 如何每 2 秒更新 TextArea(Jframe) 中的变量?

java - java中在进程中创建线程