jsoup的Elements類__js

來源:互聯網
上載者:User

一、簡介

該類是位於select包下,直接繼承自Object,所有實現的介面有Cloneable, Iterable<Element>, Collection<Element>, List<Element>

類聲明:public class Elements extends Object implements List<Element>, Cloneable

可以使用Element.select(String) 方法去得到Elements 對象。

 

二、構造方法

1、public Elements()  預設構造方法
2、public Elements(int initialCapacity) 指定一個初始容量建立一個Elements對象。

3、public Elements(Collection<Element> elements)  使用已知元素集建立一個Elements對象。

4、public Elements(List<Element> elements)  使用已知元素的List集合建立一個Elements對象。

5、public Elements(Element... elements) 使用已知元素的可變參數列表建立一個Elements對象。

 

三、方法詳細

1、public Elements clone()  複製

2、public String attr(String attributeKey) 根據鍵得到第一個匹配的元素(匹配即為有這個屬性)。

3、public boolean hasAttr(String attributeKey)   元素集中存在任何一個元素匹配(有這屬性)則返回true。

4、public Elements attr(String attributeKey, String attributeValue) 將 所有匹配attributeKey鍵的元素的值設定為attributeValue。

5、public Elements removeAttr(String attributeKey)  移除元素集中任何一個匹配的元素

6、public Elements addClass(String className)  將className增加到每一個匹配的元素的class屬性上。

7、public Elements removeClass(String className)  從每一個匹配的元素上移除該className

8、public Elements toggleClass(String className)   對每一個匹配的元素的class屬性上進行反轉。(有則移除,沒有則新增)。

9、public boolean hasClass(String className)  檢測是否有任何一個匹配的元素在class屬性有給定的className值。

10、public String val()  得到第一個匹配元素的表單的值。

11、public Elements val(String value)   對每一個匹配的元素設定表單值。

12、public String text()  得到所有匹配元素文本的綜合。該方法在某些情況下會得到重複資料。

13、public boolean hasText() 檢測是否有常值內容

14、public String html()  得到所有匹配元素的內部html的綜合。

15、public String outerHtml()  得到所有匹配元素的外部html的綜合。

16、public String toString()  得到所有匹配元素的外部html的綜合。

17、public Elements tagName(String tagName)  更新每個匹配元素的tag name.   如想把每個<i>變成<em>,可以這樣:doc.select("i").tagName("em");

18、public Elements html(String html)  設定每個匹配元素的內部html。

19、public Elements prepend(String html)   將指定html增加到每個匹配元素的內部html開頭。

20、public Elements append(String html)   將指定html增加到每個匹配元素的內部html末尾。

21、public Elements before(String html)   在每個匹配元素的外部html前面插入指定html。

22、public Elements after(String html)   在每個匹配元素的外部html後面插入指定html。

23、public Elements wrap(String html)  用指定html封裝每個匹配的元素。

例如,對於這個html:<p><b>This</b> is <b>Jsoup</b></p>,執行這個封裝:doc.select("b").wrap("<i></i>")後就變成:<p><i><b>This</b></i> is <i><b>jsoup</b></i></p>

24、public Elements unwrap()   移除匹配的元素但保留他們的內容。樣本:<div><font>One</font> <font><a href="/">Two</a></font></div>   執行 doc.select("font").unwrap()   變成:<div>One <a href="/">Two</a></div>
25、public Elements empty()  清空每個匹配元素的內容。樣本:<div><p>Hello <b>there</b></p> <p>now</p></div>  執行doc.select("p").empty()  變成<div><p></p> <p></p></div>

26、public Elements remove()  從DOM樹中移除匹配的元素。樣本:<div><p>Hello</p> <p>there</p> <img /></div>   執行doc.select("p").remove()後 變成<div> <img /></div>

27、public Elements select(String query)  根據query選取器查詢匹配的元素集。

28、public Elements not(String query)  移除匹配選取器的元素集   返回過濾後的元素集。

29、public Elements eq(int index)  根據index得到匹配的元素

30、public boolean is(String query)  檢測是否有一個元素匹配給定的選取器。

31、public Elements parents()   得到匹配元素集的所有父類元素和祖先元素集

32、public Element first()  得到第一個匹配的元素

33、public Element last()   得到最後一個匹配的元素

34、public Elements traverse(NodeVisitor nodeVisitor)  對被查詢的元素執行一次深度優先的遍曆。

35、public int size()  元素集的長度。

36、public boolean isEmpty()   檢測是否為空白

37、public boolean contains(Object o)  檢測是否包含指定對象

38、public Iterator<Element> iterator()   得到迭代器對象

39、public Object[] toArray()  將元素集轉換為數組

40、public <T> T[] toArray(T[] a) 

41、public boolean add(Element element)  新增元素

42、public boolean remove(Object o)  移除指定元素

43、public boolean containsAll(Collection<?> c)  參照java中的List或Collection用法.

44、public boolean addAll(Collection<? extends Element> c)  參照java中的List或Collection用法.

45、public boolean addAll(int index, Collection<? extends Element> c)  參照java中的List或Collection用法.

46、public boolean removeAll(Collection<?> c)     參照java中的List或Collection用法.

47、public boolean retainAll(Collection<?> c)  參照java中的List或Collection用法.

48、public void clear() 清空元素集

49、public Element get(int index)  根據索引得到指定元素

50、public Element set(int index, Element element)  根據索引設定指定元素

51、public void add(int index, Element element) 在指定位置增加元素

52、public Element remove(int index)  移除指定位置的元素

53、public int indexOf(Object o)  得到指定元素的索引(第一次出現的位置)

54、public int lastIndexOf(Object o) 得到指定元素最後一次出現的位置。

55、public ListIterator<Element> listIterator() 具體參照List<Element>

56、public ListIterator<Element> listIterator(int index)  具體參照List<Element>

57、public List<Element> subList(int fromIndex, int toIndex)  根據起始點得到子集

轉載請註明作者和出處

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.