Dmytro Pastovenskyi Dmytro Pastovenskyi - 5 months ago 40
HTML Question

Pretty HTML snippet output

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!

Answer

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();
}