Class Elements
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Element>,Collection<Element>,List<Element>,RandomAccess
public class Elements extends ArrayList<Element>
Elements, with methods that act on every element in the list.
To get an Elements object, use the Element.select(String) method.
- Author:
- Jonathan Hedley, jonathan@hedley.net
- See Also:
- Serialized Form
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description ElementsaddClass(String className)Add the class name to every matched element'sclassattribute.Elementsafter(String html)Insert the supplied HTML after each matched element's outer HTML.Elementsappend(String html)Add the supplied HTML to the end of each matched element's inner HTML.Stringattr(String attributeKey)Get an attribute value from the first matched element that has the attribute.Elementsattr(String attributeKey, String attributeValue)Set an attribute on all matched elements.Elementsbefore(String html)Insert the supplied HTML before each matched element's outer HTML.Elementsclone()Creates a deep copy of these elements.List<Comment>comments()GetCommentnodes that are direct child nodes of the selected elements.List<DataNode>dataNodes()GetDataNodenodes that are direct child nodes of the selected elements.List<String>eachAttr(String attributeKey)Get the attribute value for each of the matched elements.List<String>eachText()Get the text content of each of the matched elements.Elementsempty()Empty (remove all child nodes from) each matched element.Elementseq(int index)Get the nth matched element as an Elements object.Elementsfilter(NodeFilter nodeFilter)Perform a depth-first filtering on each of the selected elements.Elementfirst()Get the first matched element.List<FormElement>forms()Get theFormElementforms from the selected elements, if any.booleanhasAttr(String attributeKey)Checks if any of the matched elements have this attribute defined.booleanhasClass(String className)Determine if any of the matched elements have this class name set in theirclassattribute.booleanhasText()Test if any matched Element has any text content, that is not just whitespace.Stringhtml()Get the combined inner HTML of all matched elements.Elementshtml(String html)Set the inner HTML of each matched element.booleanis(String query)Test if any of the matched elements match the supplied query.Elementlast()Get the last matched element.Elementsnext()Get the immediate next element sibling of each element in this list.Elementsnext(String query)Get the immediate next element sibling of each element in this list, filtered by the query.ElementsnextAll()Get each of the following element siblings of each element in this list.ElementsnextAll(String query)Get each of the following element siblings of each element in this list, that match the query.Elementsnot(String query)Remove elements from this list that match theSelectorquery.StringouterHtml()Get the combined outer HTML of all matched elements.Elementsparents()Get all of the parents and ancestor elements of the matched elements.Elementsprepend(String html)Add the supplied HTML to the start of each matched element's inner HTML.Elementsprev()Get the immediate previous element sibling of each element in this list.Elementsprev(String query)Get the immediate previous element sibling of each element in this list, filtered by the query.ElementsprevAll()Get each of the previous element siblings of each element in this list.ElementsprevAll(String query)Get each of the previous element siblings of each element in this list, that match the query.Elementsremove()Remove each matched element from the DOM.ElementsremoveAttr(String attributeKey)Remove an attribute from every matched element.ElementsremoveClass(String className)Remove the class name from every matched element'sclassattribute, if present.Elementsselect(String query)Find matching elements within this element list.ElementstagName(String tagName)Update (rename) the tag name of each matched element.Stringtext()Get the combined text of all the matched elements.List<TextNode>textNodes()GetTextNodenodes that are direct child nodes of the selected elements.ElementstoggleClass(String className)Toggle the class name on every matched element'sclassattribute.StringtoString()Get the combined outer HTML of all matched elements.Elementstraverse(NodeVisitor nodeVisitor)Perform a depth-first traversal on each of the selected elements.Elementsunwrap()Removes the matched elements from the DOM, and moves their children up into their parents.Stringval()Get the form element's value of the first matched element.Elementsval(String value)Set the form element's value in each of the matched elements.Elementswrap(String html)Wrap the supplied HTML around each matched elements.Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
Constructor Details
-
Method Details
-
clone
Creates a deep copy of these elements. -
attr
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:
hasAttr(String)
-
hasAttr
Checks if any of the matched elements have this attribute defined.- Parameters:
attributeKey- attribute key- Returns:
- true if any of the elements have the attribute; false if none do.
-
eachAttr
Get the attribute value for each of the matched elements. If an element does not have this attribute, no value is included in the result set for that element.- Parameters:
attributeKey- the attribute name to return values for. You can add theabs:prefix to the key to get absolute URLs from relative URLs, e.g.:doc.select("a").eachAttr("abs:href").- Returns:
- a list of each element's attribute value for the attribute
-
attr
Set an attribute on all matched elements.- Parameters:
attributeKey- attribute keyattributeValue- attribute value- Returns:
- this
-
removeAttr
Remove an attribute from every matched element.- Parameters:
attributeKey- The attribute to remove.- Returns:
- this (for chaining)
-
addClass
Add the class name to every matched element'sclassattribute.- Parameters:
className- class name to add- Returns:
- this
-
removeClass
Remove the class name from every matched element'sclassattribute, if present.- Parameters:
className- class name to remove- Returns:
- this
-
toggleClass
Toggle the class name on every matched element'sclassattribute.- Parameters:
className- class name to add if missing, or remove if present, from every element.- Returns:
- this
-
hasClass
Determine if any of the matched elements have this class name set in theirclassattribute.- Parameters:
className- class name to check for- Returns:
- true if any do, false if none do
-
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:
Element.val()
-
val
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)
-
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(),eachText()
-
hasText
public boolean hasText()Test if any matched Element has any text content, that is not just whitespace.- Returns:
- true if any element has non-blank text content.
- See Also:
Element.hasText()
-
eachText
Get the text content of each of the matched elements. If an element has no text, then it is not included in the result.- Returns:
- A list of each matched element's text content.
- See Also:
Element.text(),Element.hasText(),text()
-
html
Get the combined inner HTML of all matched elements.- Returns:
- string of all element's inner HTML.
- See Also:
text(),outerHtml()
-
outerHtml
Get the combined outer HTML of all matched elements. -
toString
Get the combined outer HTML of all matched elements. Alias ofouterHtml().- Overrides:
toStringin classAbstractCollection<Element>- Returns:
- string of all element's outer HTML.
- See Also:
text(),html()
-
tagName
Update (rename) the tag name of each matched element. For example, to change each<i>to a<em>, dodoc.select("i").tagName("em");- Parameters:
tagName- the new tag name- Returns:
- this, for chaining
- See Also:
Element.tagName(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:
Element.html(String)
-
prepend
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:
Element.prepend(String)
-
append
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:
Element.append(String)
-
before
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:
Element.before(String)
-
after
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:
Element.after(String)
-
wrap
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:
Element.wrap(java.lang.String)
-
unwrap
Removes the matched elements from the DOM, and moves their children up into their parents. This has the effect of dropping the elements but keeping their children.This is useful for e.g removing unwanted formatting elements but keeping their contents.
E.g. with HTML:<div><font>One</font> <font><a href="/">Two</a></font></div>doc.select("font").unwrap();HTML =
<div>One <a href="/">Two</a></div>- Returns:
- this (for chaining)
- See Also:
Node.unwrap()
-
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:
Element.empty(),remove()
-
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
Cleanerto clean HTML.- Returns:
- this, for chaining
- See Also:
Element.empty(),empty()
-
select
Find matching elements within this element list.- Parameters:
query- ASelectorquery- Returns:
- the filtered list of elements, or an empty list if none match.
-
not
Remove elements from this list that match theSelectorquery.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
-
eq
Get the nth matched element as an Elements object.See also
ArrayList.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.
-
is
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.
-
next
Get the immediate next element sibling of each element in this list.- Returns:
- next element siblings.
-
next
Get the immediate next element sibling of each element in this list, filtered by the query.- Parameters:
query- CSS query to match siblings against- Returns:
- next element siblings.
-
nextAll
Get each of the following element siblings of each element in this list.- Returns:
- all following element siblings.
-
nextAll
Get each of the following element siblings of each element in this list, that match the query.- Parameters:
query- CSS query to match siblings against- Returns:
- all following element siblings.
-
prev
Get the immediate previous element sibling of each element in this list.- Returns:
- previous element siblings.
-
prev
Get the immediate previous element sibling of each element in this list, filtered by the query.- Parameters:
query- CSS query to match siblings against- Returns:
- previous element siblings.
-
prevAll
Get each of the previous element siblings of each element in this list.- Returns:
- all previous element siblings.
-
prevAll
Get each of the previous element siblings of each element in this list, that match the query.- Parameters:
query- CSS query to match siblings against- Returns:
- all previous element siblings.
-
parents
Get all of the parents and ancestor elements of the matched elements.- Returns:
- all of the parents and ancestor elements of the matched elements
-
first
Get the first matched element.- Returns:
- The first matched element, or
nullif contents is empty.
-
last
Get the last matched element.- Returns:
- The last matched element, or
nullif contents is empty.
-
traverse
Perform a depth-first traversal on each of the selected elements.- Parameters:
nodeVisitor- the visitor callbacks to perform on each node- Returns:
- this, for chaining
-
filter
Perform a depth-first filtering on each of the selected elements.- Parameters:
nodeFilter- the filter callbacks to perform on each node- Returns:
- this, for chaining
-
forms
Get theFormElementforms from the selected elements, if any.- Returns:
- a list of
FormElements pulled from the matched elements. The list will be empty if the elements contain no forms.
-
comments
GetCommentnodes that are direct child nodes of the selected elements.- Returns:
- Comment nodes, or an empty list if none.
-
textNodes
GetTextNodenodes that are direct child nodes of the selected elements.- Returns:
- TextNode nodes, or an empty list if none.
-
dataNodes
GetDataNodenodes that are direct child nodes of the selected elements. DataNode nodes contain the content of tags such asscript,styleetc and are distinct fromTextNodes.- Returns:
- Comment nodes, or an empty list if none.
-