简体   繁体   中英

Avoid JSoup generating whitespace when using replaceWith

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM