
- Jsoup Tutorial
- 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
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
jsoup - Set HTML
Following example will showcase use of method to set, prepend or append html to a dom element after parsing an HTML String into a Document object.
Syntax
Document document = Jsoup.parse(html); Element div = document.getElementById("sampleDiv"); div.html("<p>This is a sample content.</p>"); div.prepend("<p>Initial Text</p>"); div.append("<p>End Text</p>");
Where
document − document object represents the HTML DOM.
Jsoup − main class to parse the given HTML String.
html − HTML String.
div − Element object represent the html node element representing anchor tag.
div.html() − html(content) method replaces the element's outer html with the corresponding value.
div.prepend() − prepend(content) method adds the content before the outer html.
div.append() − append(content) method adds the content after the outer html.
Description
Element object represent a dom elment and provides various method to set, prepend or append html to a dom element.
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; public class JsoupTester { public static void main(String[] args) { String html = "<html><head><title>Sample Title</title></head>" + "<body>" + "<div id='sampleDiv'><a id='googleA' href='www.google.com'>Google</a></div>" +"</body></html>"; Document document = Jsoup.parse(html); Element div = document.getElementById("sampleDiv"); System.out.println("Outer HTML Before Modification :\n" + div.outerHtml()); div.html("<p>This is a sample content.</p>"); System.out.println("Outer HTML After Modification :\n" + div.outerHtml()); div.prepend("<p>Initial Text</p>"); System.out.println("After Prepend :\n" + div.outerHtml()); div.append("<p>End Text</p>"); System.out.println("After Append :\n" + div.outerHtml()); } }
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.
Outer HTML Before Modification : <div id="sampleDiv"> <a id="googleA" href="www.google.com">Google</a> </div> Outer HTML After Modification : <div id="sampleDiv"> <p>This is a sample content.</p> </div> After Prepend : <div id="sampleDiv"> <p>Initial Text</p> <p>This is a sample content.</p> </div> After Append : <div id="sampleDiv"> <p>Initial Text</p> <p>This is a sample content.</p> <p>End Text</p> </div> Outer HTML Before Modification : <span>Sample Content</span> Outer HTML After Modification : <span>Sample Content</span>