jazzlivetokyo
jazzlivetokyo

Reputation: 43

How to remove an element from a group of elements?

Given the code below:

HTML:

<div>a</div>
<div>b</div>
<div>c</div>

JS:

Document doc = Jsoup.parse(baseHtml);
Elements elements = doc.select("div");
elements.get(1).remove();
// remove doesn't affect elements. why?
elements.size();   // equal 3
// but this works
doc.outerHtml() // <div>a</div><div>c</div>

Do I have to use this code to get removed elements? It seems too verbose.

Document doc = Jsoup.parse(baseHtml);
Elements elements = doc.select("div");
elements.get(1).remove();
elements = doc.select("div");

Upvotes: 4

Views: 7770

Answers (2)

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

You asked why this doesn't work

elements.get(1).remove();
// remove doesn't affect elements. why?

The answer can be found in the implementation. The method you call on the element is a method implemented in the Node class.

public void remove() {
    Validate.notNull(this.parentNode);
    this.parentNode.removeChild(this);
}

As you see calling remove() removed this element from the parent (if you print the document, you will see that the element b has been removed. But that doesn't mean that has been deleted from the list of elements that the class Elements holds.

public class Elements implements List<Element>, Cloneable {
    private List<Element> contents; 

In order to do that you have to do it the way @Silviu Burcea showed you, by calling the method remove(int index) you are calling the below method which can be found implemented in the Elements class

public Element remove(int index) {
    return ((Element) this.contents.remove(index));
}

Although bare in mind, that if you do that, the only thing that you do, is to remove the i-th element from the list that Elements class contains.

Check these examples

Example 1: The size of elements is reduced, but the document remains the same

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) throws Exception {
        String baseHtml =   "<div>a</div>" +
                            "<div>b</div>" +
                            "<div>c</div>";

        Document doc = Jsoup.parse(baseHtml);
        Elements elements = doc.select("div");
        elements.remove(1);
        System.out.println(doc.outerHtml());
        System.out.println("-----------------------------------");
        System.out.println(elements.size());   
        System.out.println("-----------------------------------");
        System.out.println(doc.outerHtml()); 
    }
}

Example 2: The elements remains the same, but the document changes

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) throws Exception {
        String baseHtml =   "<div>a</div>" +
                            "<div>b</div>" +
                            "<div>c</div>";

        Document doc = Jsoup.parse(baseHtml);
        Elements elements = doc.select("div");
        elements.get(1).remove();
        System.out.println(doc.outerHtml());
        System.out.println("-----------------------------------");
        System.out.println(elements.size());   
        System.out.println("-----------------------------------");
        System.out.println(doc.outerHtml()); 
    }
}

I hope this helps to clear the confusion.

Upvotes: 1

Silviu Burcea
Silviu Burcea

Reputation: 5348

This should help you:

public class TestJsoup {

    public static void main(String[] args) {
        try {
            Document doc = Jsoup.connect("http://www.google.ro").get();
            Elements select = doc.select("div");
            System.out.println(select.size());
            select.remove(1);
            System.out.println(select.size());
        } catch (IOException ex) {
            Logger.getLogger(TestJsoup.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Upvotes: 5

Related Questions