Jsoup HTML parsing from string

Let us discuss how to parse HTML from string using Jsoup API with the help of below example.

Follow the below steps:

1. Define HTML as a string.
2. Use parse(String html) method of Jsoup class which returns Document object after processing the html string.
3. Use title() method of Document class to get the title.
4. Print the title.

Example:

JsoupParseHTMLFromString.java

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
 
/**
 * This class is used for HTML parsing from String using Jsoup.
 * @author w3schools
 */
public class JsoupParseHTMLFromString {
  public static void main(String args[]){
    //Define HTML as a string.
    String htmlString = "<html><head><title>Jsoup String HTML Test" +
		"</title></head><body><p>Parsed HTML from String." +
		"</p></body></html>";
 
    //Get Document object after parsing the html string.
    Document document = Jsoup.parse(htmlString);
 
    //Get title from document object.
    String title = document.title();
 
    //Print title.
    System.out.println("Title: " + title);
  }
}

Output:

Title: Jsoup String HTML Test

Download this example.
 
Next Topic: Jsoup overview.
Previous Topic: Jsoup HTML parsing from file example.