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:
![Formatted HTML using Java Code Formatted HTML using Java Code](https://code2care.org/ios/how-to-pretty-print-html-using-java-code/images/Formatted HTML using Java Code.jpg)
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Html,
- How to remove old 404 pages ulrs from Google crawler
- W3 HTML validator warning Unable to Determine Parse Mode
- Comprehensive 256 Ascii code table with Html Hex IBM Microsoft Key
- Align html element at the center of page vertically and horizontally
- HTML Images - Attributes and Formats
- 9 Border to DIV Element in HTML Examples
- Create HTML button that looks like a href hyperlink
- What is the doctype for HTML5?
- Simple Crossword Puzzle example using Pure HTML, CSS and JavaScript
- How to make a div tag clickable
- The author stylesheet specified in tag script is too long - document contains 21759 bytes whereas the limit is 10000 bytes
- W3 : character data is not allowed here html validation error
- How to set background color in HTML page?
- Auto Refresh Webpage after every x Second or Minute using Meta Tag?
- Default speed of Marquee tag : SCROLLAMOUNT
- Get HTML table td, tr or th inner content value with id or name attribute
- How to add multiple spaces between html page text
- Chessboard with pieces using pure HTML and CSS
- How to turn off autocomplete in input fields in HTML Form
- remove div vertical scroll
- Fibonacci series from 1 to 500 table
- Remove Html head and body tags from ckeditor source
- reCaptcha Verification expired. Check the checkbox again
- HTML5 CSS3 Color Codes List
- All directional arrows codes for HTML
More Posts:
- Pdf Text to Speech option in Mac OS X Preview App - Mac-OS-X
- [Solution] macOS Big Sur Installation: There is not enough free space, Additional Space Required - MacOS
- Android : Remove ListView Separator/divider programmatically or using xml property - Android
- Java - PatternSyntaxException - Java
- 3 Ways to convert bytes to String in Python - Python
- How to know installed version of Homebrew - MacOS
- bash: netstat: command not found - Bash
- What is an Authorization Code Grant? OAuth 2.0 - HowTos