Pretty Printing HTML String in Java using jsoup

In this post, we are going to use jsoup Java library to pretty print a HTML string.

Add jsoup library to your Java project

To use jsoup Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile 'org.jsoup:jsoup:1.13.1'

To use jsoup Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

To download the jsoup-1.13.1.jar file you can visit jsoup download page at jsoup.org/download

Pretty Printing HTML String in Java

In order to parse a HTML String firstly we need to parse the HTML String into jsoup Document object.

Then we can use different methods to get a pretty printing of the HTML String.

Example 1 using Document.html() method.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class PrettyHtmlExample1 {
    public static void main(String... args) {
        String htmlContent = "<!doctype html><html><body><div><h1>Simple Solution</h1></div></body></html>";
        Document document = Jsoup.parse(htmlContent);

        String prettyHtml = document.html();

        System.out.println(prettyHtml);
    }
}
Output:
<!doctype html>
<html>
 <head></head>
 <body>
  <div>
   <h1>Simple Solution</h1>
  </div>
 </body>
</html>

Example 2 using Document.outerHtml() method.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class PrettyHtmlExample2 {
    public static void main(String... args) {
        String htmlContent = "<!doctype html><html><body><div><h1>Simple Solution</h1></div></body></html>";

        Document document = Jsoup.parse(htmlContent);

        String prettyHtml = document.outerHtml();

        System.out.println(prettyHtml);
    }
}
Output:
<!doctype html>
<html>
 <head></head>
 <body>
  <div>
   <h1>Simple Solution</h1>
  </div>
 </body>
</html>

Example 3 using Document.toString() method.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class PrettyHtmlExample3 {
    public static void main(String... args) {
        String htmlContent = "<!doctype html><html><body><div><h1>Simple Solution</h1></div></body></html>";

        Document document = Jsoup.parse(htmlContent);

        String prettyHtml = document.toString();

        System.out.println(prettyHtml);
    }
}
Output:
<!doctype html>
<html>
 <head></head>
 <body>
  <div>
   <h1>Simple Solution</h1>
  </div>
 </body>
</html>

Happy Coding 😊

jsoup extract CSS class name of HTML element in Java

jsoup extract ID and name of HTML element in Java

jsoup extract text and attributes of HTML element in Java

jsoup extract Inner and Outer HTML of HTML Element in Java

jsoup extract JavaScript from HTML script element in Java

jsoup extract custom data attributes of HTML5 Element in Java

jsoup extract Website Title in Java

jsoup parse HTML Document from a Java String

jsoup parse HTML Document from an URL in Java

jsoup parse HTML Document from a File and InputStream in Java

Extract All Links of a web page in Java using jsoup

jsoup Get HTML elements by CSS class name in Java

Clean HTML String to get Safe HTML from Untrusted HTML in Java using jsoup

jsoup Get All HTML Elements in Java