jsoup – Loading File


jsoup – Loading from File


”;


Following example will showcase fetching an HTML from the disk using a file and then find its data.

Syntax

String url = "http://www.google.com";
Document document = Jsoup.connect(url).get();

Where

  • document − document object represents the HTML DOM.

  • Jsoup − main class to connect the url and get the HTML String.

  • url − url of the html page to load.

Description

The connect(url) method makes a connection to the url and get() method return the html of the requested url.

Example

Create the following java program using any editor of your choice in say C:/> jsoup.

JsoupTester.java

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

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

public class JsoupTester {
   public static void main(String[] args) throws IOException, URISyntaxException {
      
      URL path = ClassLoader.getSystemResource("test.htm");
      File input = new File(path.toURI());
      Document document = Jsoup.parse(input, "UTF-8");
      System.out.println(document.title());
   }
}

test.htm

Create following test.htm file in C:jsoup folder.

<html>
   <head>
      <title>Sample Title</title>
   </head>
   <body>
      <p>Sample Content</p>
   </body>
</html>

Verify the result

Compile the class using javac compiler as follows:

C:jsoup>javac JsoupTester.java

Now run the JsoupTester to see the result.

C:jsoup>java JsoupTester

See the result.

Sample Title

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *