[英]Avoid JSoup generating whitespace when using replaceWith
my goal is to turn this HTML: 我的目标是改变这个HTML:
<span style="font-family: Arial;">TEXT</span>
into this: 进入这个:
<arial>TEXT</arial>
I'm using this code: 我正在使用此代码:
public static void main(final String[] args) {
final String input = "<span style=\"font-family: Arial;\">TEXT</span>";
final Document document = Jsoup.parseBodyFragment(input);
final Tag tag = Tag.valueOf("arial");
final Element span = document.getElementsByTag("span").get(0);
final Element newElement = new Element(tag, "");
newElement.html(span.html());
span.replaceWith(newElement);
System.out.println(document.body().children());
}
But my output is: 但我的输出是:
<arial>
TEXT
</arial>
I need to avoid the whitespace surrounding the label "TEXT", but I haven't found a method or a way to specify how to generate the output without whitespaces. 我需要避免标签“TEXT”周围的空白,但我还没有找到一种方法或方法来指定如何在没有空格的情况下生成输出。
Thanks for your help 谢谢你的帮助
Finally I found the answer: 最后我找到了答案:
public static void main(final String[] args) {
final String input = "<span style=\"font-family: Arial;\">TEXT</span>";
final OutputSettings settings = new OutputSettings();
settings.prettyPrint(false);
final Document document = Jsoup.parseBodyFragment(input);
document.outputSettings(settings);
final Tag tag = Tag.valueOf("arial");
final Element span = document.getElementsByTag("span").get(0);
final Element newElement = new Element(tag, "");
newElement.html(span.html());
span.replaceWith(newElement);
System.out.print(document.body().children());
}
I needed to create an OutputSettings and set prettyPrint to false. 我需要创建一个OutputSettings并将prettyPrint设置为false。 Now the output is:
现在的输出是:
<arial>TEXT</arial>
Yay! 好极了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.