Jsoup HTML parsing from file

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

Follow the below steps:

1. Create file object using HTML file. 2. Use parse(File in, String charsetName) method of Jsoup class which returns Document object after processing the file object. 3. Use title() method of Document class to get the title. 4. Print the title.

Example:

JsoupParseHTMLFromFile.java

import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
 
/**
 * This class is used for HTML parsing from File using Jsoup.
 * @author w3spoint
 */
public class JsoupParseHTMLFromFile {
    public static void main(String args[]){
	  //Create file object using HTML file.
	  File inputFile = new File("D:\\JsoupFileTest.html");
	  Document document;
	  try {
	        //Get Document object after parsing the html file.
		document = Jsoup.parse(inputFile, "UTF-8");
 
		//Get title from document object.
		String title = document.title();
 
		//Print title.
		System.out.println("Title: " + title);
	  } catch (IOException e) {
		e.printStackTrace();
	  }		
    }
}

Output:

Title: Jsoup File HTML Test

Download this example.   Next Topic: Jsoup HTML parsing from URL example. Previous Topic: Jsoup HTML parsing from string example.

 

Please follow and like us:
Content Protection by DMCA.com