text1

text2

"; into this

text1

t" />

text1

text2

"; into this

text1

t" />

text1

text2

"; into this

text1

t"/>

Formatting snippet of HTML jericho, jTidy or JSoup?

603 views Asked by At

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?

1

There are 1 answers

2
Jonas Czech On BEST ANSWER

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>