In order to pretty-print HTML code using Java code, we can make use of the jsoup library.
Maven Dependency (add in pom.xml):<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
</dependency>
Gradle Dependency (add in build.gradle):
implementation group: 'org.jsoup', name: 'jsoup', version: '1.16.1'
Now, let's take a look at a code example.
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JavaHTMLPrettyPrint {
public static void main(String[] args) {
String html = "<html><head><title>Code2care Examples</title></head><body><h1>Welcome to Code2care</h1></body></html>";
Document document = Jsoup.parse(html);
document.outputSettings().indentAmount(4).prettyPrint(true);
String prettyHtml = doc.html();
System.out.println(prettyHtml);
}
}
Output:

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Html,
- HTML5 HELLO WORLD Example
- All directional arrows codes for HTML
- Adding Sub Headings to Bootstrap Header tags
- Create HTML button that looks like a href hyperlink
- 70+ HTTP Error Codes CheatSheet
- Comprehensive 256 Ascii code table with Html Hex IBM Microsoft Key
- How to set background color in HTML page?
- Obsolete marquee element alternatives html5
- Base 64 Index, Character and Binary Table
- Get HTML table td, tr or th inner content value with id or name attribute
- Align html element at the center of page vertically and horizontally
- How to add multiple spaces between html page text
- The author stylesheet specified in tag script is too long - document contains 21759 bytes whereas the limit is 10000 bytes
- How to pretty print HTML using Java Code
- W3 HTML validator warning Unable to Determine Parse Mode
- Hyperlink in html (anchor tag) without a underline
- reCaptcha Verification expired. Check the checkbox again
- Auto Refresh Webpage after every x Second or Minute using Meta Tag?
- Chessboard with pieces using pure HTML and CSS
- Default speed of Marquee tag : SCROLLAMOUNT
- Fibonacci series from 1 to 500 table
- 9 Border to DIV Element in HTML Examples
- How to remove old 404 pages ulrs from Google crawler
- Simple Crossword Puzzle example using Pure HTML, CSS and JavaScript
- How to make a div tag clickable
More Posts:
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer - cURL
- Add Bookmark macOS Safari - MacOS
- Save TextEdit file as a PDF - MacOS
- Android : DeviceMonitor] Sending Tracking request failed! Error - Android
- How to install PowerShell on Mac using Brew - Powershell
- Create Nested Directories using Java Code - Java
- bash get year 2021 calendar - Bash
- Calculate Volume of Ellipsoid - C-Program