使用 Jsoup 仅删除 html 标签并留下标签内的文本
Removing only an html tag and leaving behind the text inside the tag using Jsoup
只想删除内部标签"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>
请帮忙..
如果你的标签是正确的,你问如何做到这一点 Java...
String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");
...应该有帮助。
最简单的解决方法是使用 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);
}
使用StringUtils#substringBetween from Apache Commons Lang,可能会省很多力气。
String spanText = StringUtils.substringBetween(source, "<span>", "</span>");
String result = source.replaceAll("<span>.+</span>", spanText);
只想删除内部标签"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>
请帮忙..
如果你的标签是正确的,你问如何做到这一点 Java...
String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");
...应该有帮助。
最简单的解决方法是使用 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);
}
使用StringUtils#substringBetween from Apache Commons Lang,可能会省很多力气。
String spanText = StringUtils.substringBetween(source, "<span>", "</span>");
String result = source.replaceAll("<span>.+</span>", spanText);