How to remove an element from a group of elements?

前端 未结 2 1292
别那么骄傲
别那么骄傲 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<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.

    0 讨论(0)
  • 2021-01-19 07:20

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题