Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new html tag with Jsoup?

I'm having some trouble adding a new tag to my Document. For example I have:

Document doc = Jsoup.parse(htmlString);
Element table = doc.select("table").first();  

Now If I want to add a <LINK>tag with attributes (href,type,rel) to my table element, and then return the total as a string, How would I do this?

like image 704
user717572 Avatar asked Sep 29 '11 17:09

user717572


People also ask

How do you append HTML code in Java?

Using the innerHTML attribute:To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.

Can we use XPath in jsoup?

With XPath expressions it is able to select the elements within the HTML using Jsoup as HTML parser.


1 Answers

Use something like this:

Jsoup.parse(new URL(""), 0).getElementById("test").appendElement("h1").attr("id", "header").text("Welcome");

And all ".append*" methods.

like image 53
Clark Avatar answered Oct 21 '22 23:10

Clark