
- jsoup - Home
- jsoup - Overview
- jsoup - Environment Setup
- Examples - Input
- jsoup - Parsing String
- jsoup - Parsing Body
- jsoup - Loading URL
- jsoup - Loading File
- Examples - Extracting Data
- jsoup - Using DOM Methods
- jsoup - Using Selector Syntax
- jsoup - Extract Attributes
- jsoup - Extract Text
- jsoup - Extract HTML
- jsoup - Working with URLs
- Examples - Modifying Data
- jsoup - Set Attributes
- jsoup - Set HTML
- jsoup - Set Text Content
- Examples - Cleaning HTML
- jsoup - Sanitize HTML
- jsoup Useful Resources
- jsoup - Quick Guide
- jsoup - Useful Resources
- jsoup - Discussion
jsoup - Extracting HTML
Overview
Element object represent a dom elment and provides various method to get the inner html as well as outer html (complete html) of a dom element.
Syntax
Document document = Jsoup.parse(html); Element link = document.select("a").first(); System.out.println("Outer HTML: " + link.outerHtml()); System.out.println("Inner HTML: " + link.html());
Where
document − document object represents the HTML DOM.
Jsoup − main class to parse the given HTML String.
html − HTML string.
link − Element object represent the html node element representing anchor tag.
link.outerHtml() − outerHtml() method retrives the element complete html.
link.html() − html() method retrives the element inner html.
Example - Selecting Complete html of a Tag
JsoupTester.java
package com.tutorialspoint; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JsoupTester { public static void main(String[] args) { String html = "<html><head><title>Sample Title</title></head>" + "<body>" + "<p>Sample Content</p>" + "<div id='sampleDiv'><a href='www.google.com'>Google</a>" + "<h3><a>Sample</a><h3>" +"</div>" + "<div id='imageDiv' class='header'><img name='google' src='google.png' />" + "<img name='yahoo' src='yahoo.jpg' />" +"</div>" +"</body></html>"; Document document = Jsoup.parse(html); //a with href Elements links = document.select("a[href]"); for (Element link : links) { System.out.println(link.outerHtml()); } } }
Verify the result
Compile and run the JsoupTester to verify the result −
<a href="www.google.com">Google</a>
Example - Getting Inner HTML of a tag
JsoupTester.java
package com.tutorialspoint; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JsoupTester { public static void main(String[] args) { String html = "<html><head><title>Sample Title</title></head>" + "<body>" + "<p>Sample Content</p>" + "<div id='sampleDiv'><a href='www.google.com'>Google</a>" + "<h3><a>Sample</a><h3>" +"</div>" + "<div id='imageDiv' class='header'><img name='google' src='google.png' />" + "<img name='yahoo' src='yahoo.jpg' />" +"</div>" +"</body></html>"; Document document = Jsoup.parse(html); //a with href Elements links = document.select("a[href]"); for (Element link : links) { System.out.println(link.html()); } } }
Verify the result
Compile and run the JsoupTester to verify the result −
Example - Selecting HTML of a DIV
JsoupTester.java
package com.tutorialspoint; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JsoupTester { public static void main(String[] args) { String html = "<html><head><title>Sample Title</title></head>" + "<body>" + "<p>Sample Content</p>" + "<div id='sampleDiv'><a href='www.google.com'>Google</a>" + "<h3><a>Sample</a><h3>" +"</div>" + "<div id='imageDiv' class='header'><img key='google1' name='google' src='google.png' />" + "<img key='yahoo1' name='yahoo' src='yahoo.jpg' />" +"</div>" +"</body></html>"; Document document = Jsoup.parse(html); // select divs Elements divs = document.select("div"); for (Element div : divs) { System.out.println("Outer HTML: " + div.outerHtml()); System.out.println("Inner HTML: " + div.html()); } } }
Verify the result
Compile and run the JsoupTester to verify the result −
Outer HTML: <div id="sampleDiv"> <a href="www.google.com">Google</a> <h3><a>Sample</a></h3> <h3></h3> </div> Inner HTML: <a href="www.google.com">Google</a> <h3><a>Sample</a></h3> <h3></h3> Outer HTML: <div id="imageDiv" class="header"> <img key="google1" name="google" src="google.png"><img key="yahoo1" name="yahoo" src="yahoo.jpg"> </div> Inner HTML: <img key="google1" name="google" src="google.png"><img key="yahoo1" name="yahoo" src="yahoo.jpg">
Advertisements