如何使用jsoup替换标签 [英] How to replace a tag using jsoup
问题描述
我想用div
标签替换所有图像标签.我可以选择所有标签,并且知道必须使用replaceWith
.但是我无法使用它.
I want to replace all image tags with div
tag. I am able to select all tags and I know that I have to use replaceWith
. But I am unable to use it.
如果我使用TextNode
替换为<div> </div>
,它将转换为&lt;div&gt; my div &lt;/div&gt;
And if I use TextNode
for replacing it with <div> </div>
and it converts into &lt;div&gt; my div &lt;/div&gt;
我知道&lt;
和&gt;
用于<
和>
请帮助我.
推荐答案
我猜您要替换为element.replaceWith(new TextNode("<div></div>"),"");
?
A Textnode
用于文本并转义内容-这就是为什么您看到HTML实体的原因.您需要替换为标签,所以要做类似element.replaceWith(new Element(Tag.valueOf("div"), ""));
的事情.
A Textnode
is for text and escapes content - thats why you see the HTML entities. You need to replace with a tag, so do something like element.replaceWith(new Element(Tag.valueOf("div"), ""));
.
这篇关于如何使用jsoup替换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!