0

I want to format/indent snippet of HTML

String html = "<div><p>text1</p></div><div><p>text2</p></div>";

into this

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

I tried jTidy and JSoup however they adjusts my HTML with and/or or . I want to have something that would simply format part of my HTML like in example above.

I found jericho and it seems to do what I want, but I would prefer to use jTidy/JSoup.

Is it possible to do what I want with jTidy or JSoup?

Dmytro Pastovenskyi
  • 5,240
  • 5
  • 37
  • 56

1 Answers1

3

jSoup can do this:

String html = "<div><p>text1</p></div><div><p>text2</p></div>";
Document doc = Jsoup.parseBodyFragment(html);
System.out.println(doc.body().children());

Output:

<div>
 <p>text1</p>
</div>
<div>
 <p>text2</p>
</div>
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • But how can I handle situation when my snippet is part of head? or there are head and body together? – Dmytro Pastovenskyi Mar 31 '15 at 08:49
  • @Dmytro If you have complete HTML (including ``, ``, and ``) use `Jsoup.parse(html); System.out.println(doc);` Jsoup also has an XML parsing mode, which will ignore HTML requirements, and will parse anything. See [here](http://stackoverflow.com/a/9947639/4428462) – Jonas Czech Mar 31 '15 at 09:24