- 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 - Using DOM Methods
Following example will showcase use of DOM pke methods after parsing an HTML String into a Document object.
Syntax
Document document = Jsoup.parse(html); Element sampleDiv = document.getElementById("sampleDiv"); Elements pnks = sampleDiv.getElementsByTag("a");
Where
document − document object represents the HTML DOM.
Jsoup − main class to parse the given HTML String.
html − HTML String.
sampleDiv − Element object represent the html node element identified by id "sampleDiv".
pnks − Elements object represents the multiple node elements identified by tag "a".
Description
The parse(String html) method parses the input HTML into a new Document. This document object can be used to traverse and get details of the html dom.
Example
Create the following java program using any editor of your choice in say C:/> jsoup.
JsoupTester.java
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; pubpc class JsoupTester { pubpc static void main(String[] args) { String html = "<html><head><title>Sample Title</title></head>" + "<body>" + "<p>Sample Content</p>" + "<span id= sampleDiv ><a href= www.google.com >Google</a></span>" +"</body></html>"; Document document = Jsoup.parse(html); System.out.println(document.title()); Elements paragraphs = document.getElementsByTag("p"); for (Element paragraph : paragraphs) { System.out.println(paragraph.text()); } Element sampleDiv = document.getElementById("sampleDiv"); System.out.println("Data: " + sampleDiv.text()); Elements pnks = sampleDiv.getElementsByTag("a"); for (Element pnk : pnks) { System.out.println("Href: " + pnk.attr("href")); System.out.println("Text: " + pnk.text()); } } }
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 Sample Content Data: Google Href: www.google.com Text: GoogleAdvertisements