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!
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();
}
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>
I would use HTML Tidy here is an online version.
Many of the text editors have plugins or built in functionality for this.
来源:https://stackoverflow.com/questions/29196699/pretty-html-snippet-output