java - 使用 JSoup 连接 HTML 元素

标签 java html tags jsoup element

JSoup 中是否有任何方法可以加入内存中的两个或多个元素 - 即在Document树中,而不产生原始数据HTML 字符串?

例如,以下带有一些嵌套标签的 HTML div 元素

<div>This is text with <custom>a custom nested tag</custom> and some <other>text within a tag</other>, all of which should become part of the top-level </div>.

将转化为

<div>This is text with a custom nested tag and some text within a tag, all of which should become part of the top-level </div>.

本质上,上例中的嵌套标签已被删除,但其内容仍然保留,就像在原始 HTML 上运行了字符串 replace() 操作一样,然后由 JSoup 解析为 Document 对象。

整体操作可以这样编码:

public static method splice(Document document, List<String> tags) {
  for (String tag : tags) {
    // Find the tag node (Element) in the tree
    // Remove the tag node and join its content with its parent
  }
}

最佳答案

Jsoup 的 upwrap()功能就是您要寻找的。它删除元素但保留子元素。

关于java - 使用 JSoup 连接 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62336683/

相关文章:

HTML:文本、标题、副行

java - Spring + Java : Timestamp cannot be cast to Date? 从 SQL 中提取,但类型为日期。代码中没有时间戳

java - 卡夫卡连接 java.lang.NoSuchMethodError : com. google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifyingIterator;

javascript - HTML/CSS : How to make a bar on the side to take you to a certain part of the page

javascript - 如何在 jQuery 中对 div 进行动画处理,但它在该 div 中包含 div?

如果匹配,mysql替换字符串中的最后一个字符

java - 用于执行一种方法的 JUnit 套件运行程序

java - 如何创建在 JOptionPane 中打印数组的方法

javascript - AngularJS 使用 X-Editable 更新数据库

git - git 可以列出两个特定提交之间出现的标签吗?