Java HTML / XML How to - Create a new tag in the DOM with Jsoup








Question

We would like to know how to create a new tag in the DOM with Jsoup.

Answer

import java.io.File;
/*  ww  w  . ja  v a  2 s .  c om*/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Main {

    public static void main(String[] args) throws Exception {
        Document doc =  Jsoup.parse(new File("t2.html"), "UTF-8");
        doc.select("area#area1").after("<area id=\"newArea\" />");
        System.out.println(doc.html()); 
    }

}