只想删除内部标签“span”,不想删除其中的文本
<blockquote>
<span>I don’t even bring up technology.</span>
I talk about the flow of data.”
<cite>–Rick Hassman, CIO, Pella</cite>
</blockquote>
解析后应该是这样的
<blockquote>
I don’t even bring up technology.
I talk about the flow of data.”
<cite>–Rick Hassman, CIO, Pella</cite>
</blockquote>
请帮忙..
最佳答案
解决这个问题最简单的方法是使用 String.replace() 方法。
String newHtml = html.replaceAll( "<\\/?\\s*span.*?>", "");
如果您更喜欢使用 Jsoup,那么它会变得更复杂:
Document doc = Jsoup.parse(html);
for (Element e : doc.select("span")) {
Element parent = e.parent();
Element newParent = parent.clone();
newParent.empty();
for (Node n : parent.childNodes()) {
if (n instanceof Element && ((Element) n).tag().getName().equals("span")) {
newParent.append(((Element) n).html());
} else {
newParent.append(n.outerHtml());
}
}
parent.replaceWith(newParent);
}
关于java - 使用 Jsoup 仅删除 html 标签并保留标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51204088/