Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSoup Remove Elements

Tags:

java

jsoup

Even though, this may sound too basic, I would like to ask how do I remove an element from doc using Jsoup.

I tried searching for it, but no success.

Here is problem:

Elements myNewElements = doc.getElementsByAttribute("hello");

//Now I need to perform some other methods on myNewElements before removing.
//Hence..suggested method says,
doc.getElementsByAttribute("hello").remove();

This works fine. But I believe selecting same elements again and again could prove memory hungry. Is it possible ?

doc.select(myNewElements).remove();

//Try to select myNewElements from doc.

like image 313
akshayb Avatar asked May 08 '13 17:05

akshayb


People also ask

How do I delete a tag in jsoup?

Document docsoup = Jsoup. parse(htmlin); docsoup. head(). remove();

What does jsoup clean do?

clean. Creates a new, clean document, from the original dirty document, containing only elements allowed by the safelist. The original document is not modified. Only elements from the dirty document's body are used.

Can jsoup parse JavaScript?

Jsoup parses the source code as delivered from the server (or in this case loaded from file). It does not invoke client-side actions such as JavaScript or CSS DOM manipulation.

What is jsoup parse?

jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.


1 Answers

If you didn't add any new elements that match your inital select, you don't need to select the elements again.

Each element in elements has a reference to its parent and the remove() method just tells the parent to remove that child element.

In essence, just doing:

myNewElements.remove()

should work.

like image 131
Francisco Paulo Avatar answered Sep 28 '22 10:09

Francisco Paulo