You get a single line because text()
remove all whitepace characters.
But you can use a StringBuilder
and insert each line there:
final String html = "<p>test1 <b>test2</b> test 3 </p>"
+ "<p>test4 </p>";
Document doc = Jsoup.parse(html);
StringBuilder sb = new StringBuilder();
for( Element element : doc.select("p") )
{
/*
* element.text() returns the text of this element (= without tags).
*/
sb.append(element.text()).append('\n');
}
System.out.println(sb.toString().trim());
Output:
test1 test2 test 3
test4