Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Element with Jsoup doesn't work

Tags:

java

html

css

jsoup

i want to remove some elements (CSS--tags) in the head of a html file. I tried it like this:

Document doc = Jsoup.parse(htmlString);
Element head = doc.head();
Elements headChildren = head.children();
for (Element el : headChildren) {
    if (el.attr("type").contains("text/css") || el.attr("rel").contains("stylesheet")){
       Log.d("HTML", "elements-before: " +  el.nodeName()); //Log prints 7 elments
       el.remove();

    }
}

for (Element el : headChildren ) {
    if (el.attr("type").contains("text/css") || el.attr("rel").contains("stylesheet")){
       Log.d("HTML", "elements-after: " +  el.nodeName()); //Log prints 7 elments again
    }
}

I really don't know where my fault is. Please help me out.

Thanks in advance for any advice!!

like image 693
A.D. Avatar asked Nov 14 '25 23:11

A.D.


1 Answers

You are removing the Element from the Document object where they are located, not the Elements collection. Thus, when you print each Element the second time, they are still in the Elements collection, but not in the Document.

The Jsoup classes Element, Comment, Document and so on are all subclasses of the Node class, which represents a node in the DOM tree. The method remove() is an inherited method from the Node class, and it removes the node from the DOM tree.

like image 73
Daniel B Avatar answered Nov 17 '25 18:11

Daniel B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!