jsoup – strip all formatting and link tags, keep text only

With Jsoup:

final String html = "<p> <span> foo </span> <em> bar <a> foobar </a> baz </em> </p>";
Document doc = Jsoup.parse(html);

System.out.println(doc.text());

Output:

foo bar foobar baz

If you want only the text of p-tag, use this instead of doc.text():

doc.select("p").text();

… or only body:

doc.body().text();

Linebreak:

final String html = "<p><strong>Tarthatatlan biztonsági viszonyok</strong></p>"
        + "<p><strong>Tarthatatlan biztonsági viszonyok</strong></p>";
Document doc = Jsoup.parse(html);

for( Element element : doc.select("p") )
{
    System.out.println(element.text());
    // eg. you can use a StringBuilder and append lines here ...
}

Output:

Tarthatatlan biztonsági viszonyok  
Tarthatatlan biztonsági viszonyok

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)