I've a snippet of HTML <div><p>text1</p></div><div><p>text1</p></div>
I want to make it pretty like this
<div>
<p>text1</p>
</div>
<div>
<p>text1</p>
</div>
What would be most simple way to do it? (I've looked on transform and jsoup) but not sure what would be really smart to use. Thanks!
You can use Jsoup like
String html = "<div><p>text1</p></div><div><p>text1</p></div>";
Document doc = Jsoup.parseBodyFragment(html);
But this will wrap your text into
<html>
<head></head>
<body>
..
</body>
</html>
To get rid of this part you can get part from <body>
like
System.out.println(doc.body().html());
which prints
<div>
<p>text1</p>
</div>
<div>
<p>text1</p>
</div>
If you want to increase indentation you can set it earlier with
doc.outputSettings().indentAmount(4);
now result will look like
<div>
<p>text1</p>
</div>
<div>
<p>text1</p>
</div>
jTidy could fit for this task - http://jtidy.sourceforge.net/howto.html
public String prettyPrintHTML(String rawHTML)
{
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
// HTML to DOM
Document htmlDOM = tidy.parseDOM(new ByteArrayInputStream(rawHTML.getBytes()), null);
// Pretty Print
OutputStream out = new ByteArrayOutputStream();
tidy.pprint(htmlDOM, out);
return out.toString();
}
I would use HTML Tidy here is an online version.
Many of the text editors have plugins or built in functionality for this.
Sublime Text
BBEdit
Coda
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With