Pretty HTML snippet output

I have an HTML fragment <div><p>text1</p></div><div><p>text1</p></div>

I want to do it something like this:

 <div> <p>text1</p> </div> <div> <p>text1</p> </div> 

What would be the easiest way to do this? (I looked at transform and jsoup), but not sure if it would be really wise to use. Thanks!

+7
java html pretty-print jsoup transform
source share
3 answers

jTidy can 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(); } 
+2
source share

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> 
+12
source share

I would use HTML Tidy here online version .

Many text editors have plugins or built-in functions for this.

High text

BBEdit

Coda

+2
source share

All Articles