4

I want to get this code;

<p>Text<br />
New Text<br />
Second Text<br />
Third Text</p>

With this code;

Elements pResult = p.getElementsByTag("p");
System.out.println(pResult.text());

I show this > Text New Text Second Text Third Text

But i want

<p>Text <br>New text<br>Second Text<br>Third Text</p>

Because of <br> tag

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
  • Possible duplicate of [How do I preserve line breaks when using jsoup to convert html to plain text?](http://stackoverflow.com/questions/5640334/how-do-i-preserve-line-breaks-when-using-jsoup-to-convert-html-to-plain-text/6031463#6031463) – edasssus Apr 08 '16 at 11:27
  • 1
    I have no experience Using jsoup, but my guess is following: When you use .text(), this should return plain text content inside

    tag, without tags. I think what you need to do is to use .html() function: pResult.html(), then replace all
    inside your string with newline character and print it. (or you could just split string on
    and print afterwards)

    – Tornike Shavishvili Apr 08 '16 at 11:31
  • As suggested by @blindProgrammer following provide the output you want `System.out.println(pResult.html().replaceAll("
    ", "\n"))`.
    – SubOptimal Apr 08 '16 at 11:39
  • Edit: as Documentation states, to obtain "

    Text
    New text
    Second Text
    Third Text

    " <--this, you use following: pResult.outerHtml()
    – Tornike Shavishvili Apr 08 '16 at 13:50

2 Answers2

0

Try like this :

 public class Test {
    public static void main(String[] args) {
String s="<p>Text<br /> New Text<br />Second Text<br />Third Text</p>";
        Document document = Jsoup.parse(s);
        document.outputSettings(new Document.OutputSettings().prettyPrint(false));
        document.select("br").append("\\n");
        document.select("p").prepend("\\n\\n");
        String s1 = document.html().replaceAll("\\\\n", "\n");
        System.out.println(Jsoup.clean(s1, "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false)));
    }
}
soorapadman
  • 4,451
  • 7
  • 35
  • 47
0

Try this code:

String s="<p>Text<br />\nNew Text<br />\nSecond Text<br />\nThird Text</p>";

System.out.println(Jsoup.parse(s).select("p").outerHtml());

OUTPUT

<p>Text<br> New Text<br> Second Text<br> Third Text</p>

Jsoup 1.8.3

Stephan
  • 41,764
  • 65
  • 238
  • 329