You can use Jsoup , for example
String html = "<div><p>text1</p></div><div><p>text1</p></div>"; Document doc = Jsoup.parseBodyFragment(html);
But it will turn your text into
<html> <head></head> <body> .. </body> </html>
To get rid of this part, you can get the part from <body> as
System.out.println(doc.body().html());
which prints
<div> <p>text1</p> </div> <div> <p>text1</p> </div>
If you want to increase the indent, you can set it earlier
doc.outputSettings().indentAmount(4);
now the result will look like
<div> <p>text1</p> </div> <div> <p>text1</p> </div>
Pshemo
source share