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!
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:
- How to create SharePoint Online List Item using REST API - SharePoint
- Fix: Ubuntu (Linux) - bash: sudo: command not found error - Ubuntu
- Python String contains substring equivalent example - Python
- How to Start Jupyter Notebook on Mac - MacOS
- Notepad++ Display CSV in Columns - NotepadPlusPlus
- How to share SharePoint site or document with all users in organization - SharePoint
- Bash Command to Check IP Address - Bash
- Java 8 Predicate default and() function Example - Java