jsoupjtidyjericho-html-parser

Formatting snippet of HTML jericho, jTidy or JSoup?


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?


Solution

  • 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>