简体   繁体   中英

Preserving the <br> tags when cleaning with Jsoup

For the input text:

<p>Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?

I run the following code:

Whitelist list = Whitelist.simpleText().addTags("br");
// Some other code...
// plaintext is the string shown above
retVal = Jsoup.clean(plaintext, StringUtils.EMPTY, list,
            new Document.OutputSettings().prettyPrint(false));

I get the output:

Arbit string <b>of</b>

text. <em>What</em> to <strong>do</strong> with it?

I don't want Jsoup to convert the <br> tags to line breaks, I want to keep them as-is. How can I do that?

Try this:

Document doc2deal = Jsoup.parse(inputText);
doc2deal.select("br").append("br"); //or append("<br>")

This is not reproducible for me. Using Jsoup 1.8.3 and this code:

String html = "<p>Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?";
String cleaned = Jsoup.clean(html, 
        "", 
        Whitelist.simpleText().addTags("br"),
        new Document.OutputSettings().prettyPrint(false));
System.out.println(cleaned);

I get the following output:

Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?

Your problem must be somewhere else I guess.

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