Reputation: 375
my goal is to turn this 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.
Thanks for your help
Upvotes: 1
Views: 1801
Reputation: 375
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. Now the output is:
<arial>TEXT</arial>
Yay!
Upvotes: 3