2012-06-16 77 views
4

考慮下面的代碼:如何從一組元素中刪除元素?

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> 

我必須使用此代碼來獲取刪除的元素?這看起來太冗長了。

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

下面的答案有幫助嗎?如果是,請接受一個,所以這個話題可以被視爲封閉。如果沒有,請讓社區知道,以便我們可以提供進一步的幫助。 – alkis

回答

5

這會幫助你:

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); 
     } 
    } 
} 
1

你問爲什麼

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

答案可以在實施中找到這不起作用。 您在元素上調用的方法是在 類Node類中實現的一種方法。

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

正如你看到的調用remove()刪除從父 這個元素(如果你打印文檔,你會看到該元素b一直 刪除。但是,這並不意味着已經從刪除該類Elements持有 元素的列表。

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

爲了做到這一點,你必須做的方式@Silviu Burcea表明你, 通過調用方法remove(int index)你調用下面的方法 可在Elements

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

雖然心中裸地發現實現,如果你這樣做,你唯一要做的,就是 除去第i個元素來自Elements類包含的列表。

檢查這些例子

實施例1:元件的尺寸減小,但該文檔仍然是相同的

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()); 
    } 
} 

實施例2:元素保持不變,但該文檔變化

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()); 
    } 
} 

我希望這有助於澄清confu錫永。