Dmytro Pastovenskyi Dmytro Pastovenskyi - 1 year ago 95
HTML Question

Pretty HTML snippet output

I've a snippet of HTML


I want to make it pretty like this


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 Source

jTidy could fit for this task -

public String prettyPrintHTML(String rawHTML)
    Tidy tidy = new Tidy();

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