Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save documentprocessing/bb4aa6942d5fb64ce43db161f8621ab9 to your computer and use it in GitHub Desktop.

Select an option

Save documentprocessing/bb4aa6942d5fb64ce43db161f8621ab9 to your computer and use it in GitHub Desktop.
Modify HTML of an Element in Java with jsoup API
Element div = doc.select("div").first(); // <div></div>
div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p></div>
div.prepend("<p>First</p>");
div.append("<p>Last</p>");
// now: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>
Element span = doc.select("span").first(); // <span>One</span>
span.wrap("<li><a href='http://example.com/'></a></li>");
// now: <li><a href="http://example.com"><span>One</span></a></li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment