Jsoup Tutorial
Selected Reading
- jsoup - Discussion
- jsoup - Useful Resources
- jsoup - Quick Guide
- jsoup - Sanitize HTML
- jsoup - Set Text Content
- jsoup - Set HTML
- jsoup - Set Attributes
- jsoup - Working with URLs
- jsoup - Extract HTML
- jsoup - Extract Text
- jsoup - Extract Attributes
- jsoup - Using Selector Syntax
- jsoup - Using DOM Methods
- jsoup - Loading File
- jsoup - Loading URL
- jsoup - Parsing Body
- jsoup - Parsing String
- jsoup - Environment Setup
- jsoup - Overview
- jsoup - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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; pubpc class JsoupTester { pubpc 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 TitleAdvertisements