public class

Elements

extends Object
implements Cloneable List<E>
java.lang.Object
   ↳ org.jsoup.select.Elements

Class Overview

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

Summary

Public Constructors
Elements()
Elements(Collection<Element> elements)
Elements(List<Element> elements)
Elements(Element... elements)
Public Methods
void add(int index, Element element)
boolean add(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.
Elements after(String html)
Insert the supplied HTML after each matched element's outer HTML.
Elements append(String html)
Add the supplied HTML to the end of each matched element's inner HTML.
String attr(String attributeKey)
Get an attribute value from the first matched element that has the attribute.
Elements attr(String attributeKey, String attributeValue)
Set an attribute on all matched elements.
Elements before(String html)
Insert the supplied HTML before each matched element's outer HTML.
void clear()
Elements clone()
boolean contains(Object o)
boolean containsAll(Collection<?> c)
Elements empty()
Empty (remove all child nodes from) each matched element.
Elements eq(int index)
Get the nth matched element as an Elements object.
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.
boolean hasText()
int hashCode()
String html()
Get the combined inner HTML of all matched elements.
Elements html(String html)
Set the inner HTML of each matched element.
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)
Elements not(String query)
Remove elements from this list that do not match the Selector query.
String outerHtml()
Get the combined outer HTML of all matched elements.
Elements parents()
Get all of the parents and ancestor elements of the matched elements.
Elements prepend(String html)
Add the supplied HTML to the start of each matched element's inner HTML.
boolean remove(Object o)
Elements remove()
Remove each matched element from the DOM.
Element remove(int index)
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)
Elements tagName(String tagName)
Update the tag name of each matched element.
String text()
Get the combined text of all the matched elements.
<T> T[] toArray(T[] a)
Object[] toArray()
String toString()
Get the combined outer HTML of all matched elements.
Elements toggleClass(String className)
Toggle the class name on every matched element's class attribute.
String val()
Get the form element's value of the first matched element.
Elements val(String value)
Set the form element's value in each of the matched elements.
Elements wrap(String html)
Wrap the supplied HTML around each matched elements.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Iterable
From interface java.util.Collection
From interface java.util.List

Public Constructors

public Elements ()

public Elements (Collection<Element> elements)

public Elements (List<Element> elements)

public Elements (Element... elements)

Public Methods

public void add (int index, Element element)

public boolean add (Element element)

public boolean addAll (Collection<? extends Element> c)

public boolean addAll (int index, Collection<? extends Element> c)

public Elements addClass (String className)

Add the class name to every matched element's class attribute.

Parameters
className class name to add
Returns
  • this

public Elements after (String html)

Insert the supplied HTML after each matched element's outer HTML.

Parameters
html HTML to insert after each element
Returns
  • this, for chaining
See Also

public Elements append (String html)

Add the supplied HTML to the end of each matched element's inner HTML.

Parameters
html HTML to add inside each element, after the existing HTML
Returns
  • this, for chaining
See Also

public String attr (String attributeKey)

Get an attribute value from the first matched element that has the attribute.

Parameters
attributeKey The attribute key.
Returns
  • The attribute value from the first matched element that has the attribute.. If no elements were matched (isEmpty() == true), or if the no elements have the attribute, returns empty string.
See Also

public Elements attr (String attributeKey, String attributeValue)

Set an attribute on all matched elements.

Parameters
attributeKey attribute key
attributeValue attribute value
Returns
  • this

public Elements before (String html)

Insert the supplied HTML before each matched element's outer HTML.

Parameters
html HTML to insert before each element
Returns
  • this, for chaining
See Also

public void clear ()

public Elements clone ()

public boolean contains (Object o)

public boolean containsAll (Collection<?> c)

public Elements empty ()

Empty (remove all child nodes from) each matched element. This is similar to setting the inner HTML of each element to nothing.

E.g. HTML: <div><p>Hello <b>there</b></p> <p>now</p></div>
doc.select("p").empty();
HTML = <div><p></p> <p></p></div>

Returns
  • this, for chaining
See Also

public Elements eq (int index)

Get the nth matched element as an Elements object.

See also get(int) to retrieve an 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.

public boolean equals (Object o)

public Element first ()

Get the first matched element.

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

public Element get (int index)

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.

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

public boolean hasText ()

public int hashCode ()

public String html ()

Get the combined inner HTML of all matched elements.

Returns
  • string of all element's inner HTML.

public Elements html (String html)

Set the inner HTML of each matched element.

Parameters
html HTML to parse and set into each matched element.
Returns
  • this, for chaining
See Also

public int indexOf (Object o)

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.

public boolean isEmpty ()

public Iterator<Element> iterator ()

public Element last ()

Get the last matched element.

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

public int lastIndexOf (Object o)

public ListIterator<Element> listIterator ()

public ListIterator<Element> listIterator (int index)

public Elements not (String query)

Remove elements from this list that do not match the Selector query.

E.g. HTML: <div class=logo>One</div> <div>Two</div>
Elements divs = doc.select("div").not("#logo");
Result: divs: [<div>Two</div>]

Parameters
query the selector query whose results should be removed from these elements
Returns
  • a new elements list that contains only the filtered results

public String outerHtml ()

Get the combined outer HTML of all matched elements.

Returns
  • string of all element's outer HTML.
See Also

public Elements parents ()

Get all of the parents and ancestor elements of the matched elements.

public Elements prepend (String html)

Add the supplied HTML to the start of each matched element's inner HTML.

Parameters
html HTML to add inside each element, before the existing HTML
Returns
  • this, for chaining
See Also

public boolean remove (Object o)

public Elements remove ()

Remove each matched element from the DOM. This is similar to setting the outer HTML of each element to nothing.

E.g. HTML: <div><p>Hello</p> <p>there</p> <img /></div>
doc.select("p").remove();
HTML = <div> <img /></div>

Note that this method should not be used to clean user-submitted HTML; rather, use Cleaner to clean HTML.

Returns
  • this, for chaining
See Also

public Element remove (int index)

public boolean removeAll (Collection<?> c)

public Elements removeAttr (String attributeKey)

Remove an attribute from every matched element.

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

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

public boolean retainAll (Collection<?> c)

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.

public Element set (int index, Element element)

public int size ()

public List<Element> subList (int fromIndex, int toIndex)

public Elements tagName (String tagName)

Update the tag name of each matched element. For example, to change each <i> to a <em>, do doc.select("i").tagName("em");

Parameters
tagName the new tag name
Returns
  • this, for chaining
See Also

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

public T[] toArray (T[] a)

public Object[] toArray ()

public String toString ()

Get the combined outer HTML of all matched elements. Alias of outerHtml().

Returns
  • string of all element's outer HTML.
See Also

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

public String val ()

Get the form element's value of the first matched element.

Returns
  • The form element's value, or empty if not set.
See Also

public Elements val (String value)

Set the form element's value in each of the matched elements.

Parameters
value The value to set into each matched element
Returns
  • this (for chaining)

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 arbitrarily deep.
Returns
  • this (for chaining)
See Also