Load and Parse HTML file using JSoup

If you want to parse the html file from your local disk you can use the static Jsoup.parse(File in, String charsetName) or Jsoup.parse(File in, String charsetName, String baseUri) method. Also you can manipulate the html content.

If an error occurs while loading the file, it will throw an IOException. baseUri parameter is used by the parser to resolve relative URLs in the document ( relative url in href tag). You can also pass empty string if you are not concern about baseUri.

Another method parse(File in, String charsetName) also use to parse html file from you file system, which uses the file’s location as the baseUri.

JSoupParseHTML.java

package com.ehowtonow.jsoup;

import java.io.File;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JSoupParseHTML {

	public static void main(String[] args) {

		try {
			File htmlFileinput = new File("C:\\ehowtonow\\jsoup.html");
			Document htmlDocument = Jsoup.parse(htmlFileinput, "UTF-8");
			System.out.println(htmlDocument.title());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Output

How to parse HTML String using JSoup – eHowToNow – JSoup


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Load and Parse HTML from url using JSoup
  2. JSoup to parse body fragment
  3. How to parse HTML String using JSoup
  4. JSOUP HTML Parser Introduction
  5. JSoup DOM methods
  6. Load and Parse HTML file using JSoup

Be the first to comment

Leave a Reply

Your email address will not be published.


*