In this post under Jsoup, I will explain with example how to parse a html fragment as a body of a new html document.
Suppose you have a html fragment as shown below
<a href='wwww.google.com'/>
Which you want as body of a new html document as shown below for your reference
<html>
<head></head>
<body>
<a href="wwww.google.com"></a>
</body>
</html>
You can achieve this with the help of static method “parseBodyFragment” of Jsoup class as shown below
Main Code
1 import org.jsoup.Jsoup;
2 import org.jsoup.nodes.Document;
3
4 public class JsoupDemo2 {
5 public static void main(String[] args) {
6 String html = "<a href='wwww.google.com'/>";
7 Document document = Jsoup.parseBodyFragment(html);
8 System.out.println(document.html());
9 System.out.println("----------------------------");
10 System.out.println(document.body().html());
11 }
12 }
In the above code, at line 6 we declare a variable “html” with the html fragment as its value.
At line 7, we call “parseBodyFragment” method and pass the variable “html” as a argument. This method will create and return a Document instance named “document”.
This Document instance “document” will represent the new html document with the given html fragment as the only child of body element in the new html document.
At line 8, we print the entire html document
At line 10, we print only the body of the new html document.
Below is the output
Output
<html>
<head></head>
<body>
<a href="wwww.google.com"></a>
</body>
</html>
----------------------------
<a href="wwww.google.com"></a>