java 使用 jsoup 替换 HTML 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12475213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:03:25 来源:igfitidea点击:
Replace HTML tags using jsoup
提问by Hardik Lotiya
Here is my code
这是我的代码
String html = "<font>fsdfs<font>dfsdf</font>dasdasd</font>";
Document doc = Jsoup.parse(html);
Elements elements = doc.select("font");
for(Element element : elements)
{
element.replaceWith(new Element(Tag.valueOf("span"),"").html(element.html()));
}
System.out.println(doc.html());
I want to replace font tag and put span tag. In this it will replace first font tag but not second tag
我想替换字体标签并放置跨度标签。在这它将替换第一个字体标签而不是第二个标签
回答by ollo
You can replace the Tag like this too:
您也可以像这样替换标签:
String html = "<font>fsdfs<font>dfsdf</font>dasdasd</font>";
Document doc = Jsoup.parse(html);
Elements elements = doc.select("font");
// rename all 'font'-tags to 'span'-tags, will also keep attributs etc.
elements.tagName("span");
System.out.println(doc.html());