How to remove an element from a group of elements?

前端 未结 2 1325
别那么骄傲
别那么骄傲 2021-01-19 06:56

Given the code below:

HTML:

a
b
c&
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-19 07:04

    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, Cloneable {
        private List 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 =   "
    a
    " + "
    b
    " + "
    c
    "; 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 =   "
    a
    " + "
    b
    " + "
    c
    "; 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.

提交回复
热议问题