org.jsoup.select
Class Elements

java.lang.Object
  extended by org.jsoup.select.Elements
All Implemented Interfaces:
Iterable<Element>, Collection<Element>, List<Element>

public class Elements
extends Object
implements List<Element>

A list of Elements, with methods that act on every element in the list

Author:
Jonathan Hedley, [email protected]

Constructor Summary
Elements()
           
Elements(Collection<Element> elements)
           
Elements(Element... elements)
           
Elements(List<Element> elements)
           
 
Method Summary
 boolean add(Element element)
           
 void add(int index, Element element)
           
 boolean addAll(Collection<? extends Element> c)
           
 boolean addAll(int index, Collection<? extends Element> c)
           
 Elements addClass(String className)
          Add the class name to every matched element's class attribute.
 String attr(String attributeKey)
          Get the attribute value of the first matched element.
 Elements attr(String attributeKey, String attributeValue)
          Set an attribute on all matched elements.
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 Elements eq(int index)
          Reduce the matched elements to one element
 boolean equals(Object o)
           
 Element first()
          Get the first matched element.
 Element get(int index)
           
 boolean hasAttr(String attributeKey)
          Checks if any of the matched elements have this attribute set.
 boolean hasClass(String className)
          Determine if any of the matched elements have this class name set in their class attribute.
 int hashCode()
           
 boolean hasText()
           
 int indexOf(Object o)
           
 boolean is(String query)
          Test if any of the matched elements match the supplied query.
 boolean isEmpty()
           
 Iterator<Element> iterator()
           
 Element last()
          Get the last matched element.
 int lastIndexOf(Object o)
           
 ListIterator<Element> listIterator()
           
 ListIterator<Element> listIterator(int index)
           
 Element remove(int index)
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> c)
           
 Elements removeAttr(String attributeKey)
          Remove an attribute from every matched element.
 Elements removeClass(String className)
          Remove the class name from every matched element's class attribute, if present.
 boolean retainAll(Collection<?> c)
           
 Elements select(String query)
          Find matching elements within this element list.
 Element set(int index, Element element)
           
 int size()
           
 List<Element> subList(int fromIndex, int toIndex)
           
 String text()
          Get the combined text of all the matched elements.
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 Elements toggleClass(String className)
          Toggle the class name on every matched element's class attribute.
 Elements wrap(String html)
          Wrap the supplied HTML around each matched elements.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Elements

public Elements()

Elements

public Elements(Collection<Element> elements)

Elements

public Elements(List<Element> elements)

Elements

public Elements(Element... elements)
Method Detail

attr

public String attr(String attributeKey)
Get the attribute value of the first matched element.

Parameters:
attributeKey - The attribute key.
Returns:
The attribute value from the first matched element. If no elements were matched (isEmpty() == true), or if the first element does not have the attribute, returns empty string.
See Also:
hasAttr(String)

hasAttr

public boolean hasAttr(String attributeKey)
Checks if any of the matched elements have this attribute set.

Parameters:
attributeKey - attribute key
Returns:
true if any of the elements have the attribute; false if none do.

attr

public Elements attr(String attributeKey,
                     String attributeValue)
Set an attribute on all matched elements.

Parameters:
attributeKey - attribute key
attributeValue - attribute value
Returns:
this

removeAttr

public Elements removeAttr(String attributeKey)
Remove an attribute from every matched element.

Parameters:
attributeKey - The attribute to remove.
Returns:
this (for chaining)

addClass

public Elements addClass(String className)
Add the class name to every matched element's class attribute.

Parameters:
className - class name to add
Returns:
this

removeClass

public Elements removeClass(String className)
Remove the class name from every matched element's class attribute, if present.

Parameters:
className - class name to remove
Returns:
this

toggleClass

public Elements toggleClass(String className)
Toggle the class name on every matched element's class attribute.

Parameters:
className - class name to add if missing, or remove if present, from every element.
Returns:
this

hasClass

public boolean hasClass(String className)
Determine if any of the matched elements have this class name set in their class attribute.

Parameters:
className - class name to check for
Returns:
true if any do, false if none do

text

public String text()
Get the combined text of all the matched elements.

Note that it is possible to get repeats if the matched elements contain both parent elements and their own children, as the Element.text() method returns the combined text of a parent and all its children.

Returns:
string of all text: unescaped and no HTML.
See Also:
Element.text()

hasText

public boolean hasText()

wrap

public Elements wrap(String html)
Wrap the supplied HTML around each matched elements. For example, with HTML <p><b>This</b> is <b>Jsoup</b></p>, doc.select("b").wrap("<i></i>"); becomes <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>

Parameters:
html - HTML to wrap around each element, e.g. <div class="head"></div>. Can be arbitralily deep.
Returns:
this (for chaining)
See Also:
Element.wrap(java.lang.String)

select

public Elements select(String query)
Find matching elements within this element list.

Parameters:
query - A selector query
Returns:
the filtered list of elements, or an empty list if none match.

eq

public Elements eq(int index)
Reduce the matched elements to one element

Parameters:
index - the (zero-based) index of the element in the list to retain
Returns:
Elements containing only the specified element, or, if that element did not exist, an empty list.

is

public boolean is(String query)
Test if any of the matched elements match the supplied query.

Parameters:
query - A selector
Returns:
true if at least one element in the list matches the query.

first

public Element first()
Get the first matched element.

Returns:
The first matched element, or null if contents is empty;

last

public Element last()
Get the last matched element.

Returns:
The last matched element, or null if contents is empty.

size

public int size()
Specified by:
size in interface Collection<Element>
Specified by:
size in interface List<Element>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<Element>
Specified by:
isEmpty in interface List<Element>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<Element>
Specified by:
contains in interface List<Element>

iterator

public Iterator<Element> iterator()
Specified by:
iterator in interface Iterable<Element>
Specified by:
iterator in interface Collection<Element>
Specified by:
iterator in interface List<Element>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<Element>
Specified by:
toArray in interface List<Element>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<Element>
Specified by:
toArray in interface List<Element>

add

public boolean add(Element element)
Specified by:
add in interface Collection<Element>
Specified by:
add in interface List<Element>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<Element>
Specified by:
remove in interface List<Element>

containsAll

public boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<Element>
Specified by:
containsAll in interface List<Element>

addAll

public boolean addAll(Collection<? extends Element> c)
Specified by:
addAll in interface Collection<Element>
Specified by:
addAll in interface List<Element>

addAll

public boolean addAll(int index,
                      Collection<? extends Element> c)
Specified by:
addAll in interface List<Element>

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<Element>
Specified by:
removeAll in interface List<Element>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<Element>
Specified by:
retainAll in interface List<Element>

clear

public void clear()
Specified by:
clear in interface Collection<Element>
Specified by:
clear in interface List<Element>

equals

public boolean equals(Object o)
Specified by:
equals in interface Collection<Element>
Specified by:
equals in interface List<Element>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<Element>
Specified by:
hashCode in interface List<Element>
Overrides:
hashCode in class Object

get

public Element get(int index)
Specified by:
get in interface List<Element>

set

public Element set(int index,
                   Element element)
Specified by:
set in interface List<Element>

add

public void add(int index,
                Element element)
Specified by:
add in interface List<Element>

remove

public Element remove(int index)
Specified by:
remove in interface List<Element>

indexOf

public int indexOf(Object o)
Specified by:
indexOf in interface List<Element>

lastIndexOf

public int lastIndexOf(Object o)
Specified by:
lastIndexOf in interface List<Element>

listIterator

public ListIterator<Element> listIterator()
Specified by:
listIterator in interface List<Element>

listIterator

public ListIterator<Element> listIterator(int index)
Specified by:
listIterator in interface List<Element>

subList

public List<Element> subList(int fromIndex,
                             int toIndex)
Specified by:
subList in interface List<Element>


Copyright © 2009-2010 Jonathan Hedley. All Rights Reserved.