Google News
logo
Jsoup - Interview Questions
How can you serialize HTML documents using Jsoup?
In Jsoup, you can serialize HTML documents to a string using the toString() method of the Document class. This method returns the HTML content of the document as a string. Here's how you can serialize an HTML document using Jsoup:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Main {
    public static void main(String[] args) {
        // Parse HTML from a string or another source
        String html = "<html><head><title>Example</title></head><body><p>Hello, Jsoup!</p></body></html>";
        Document doc = Jsoup.parse(html);
        
        // Serialize the HTML document to a string
        String serializedHtml = doc.toString();
        
        // Print the serialized HTML
        System.out.println(serializedHtml);
    }
}?

In this example, we first parse an HTML document from a string using Jsoup.parse(). Then, we call the toString() method on the Document object doc to serialize it to a string. Finally, we print the serialized HTML string to the console.

The toString() method serializes the HTML document including the document type declaration (<!DOCTYPE>), the HTML root element (<html>), and all its children elements.

Additionally, Jsoup allows you to customize the serialization of HTML documents by configuring the output settings of the Document object. For example, you can control indentation, pretty-printing, and other formatting options. Here's an example of how you can customize the output settings:
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.indentAmount(4); // Set indentation to 4 spaces
outputSettings.prettyPrint(true); // Enable pretty-printing

doc.outputSettings(outputSettings);

String serializedHtml = doc.toString();?

By configuring the output settings before serializing the document, you can control the formatting of the serialized HTML string according to your preferences.
Advertisement