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/