public class Elements extends java.util.ArrayList<Element>
Element
s, with methods that act on every element in the list.
To get an Elements
object, use the Element.select(String)
method.
Constructor and Description |
---|
Elements() |
Elements(java.util.Collection<Element> elements) |
Elements(Element... elements) |
Elements(int initialCapacity) |
Elements(java.util.List<Element> elements) |
Modifier and Type | Method and Description |
---|---|
Elements |
addClass(java.lang.String className)
Add the class name to every matched element's
class attribute. |
Elements |
after(java.lang.String html)
Insert the supplied HTML after each matched element's outer HTML.
|
Elements |
append(java.lang.String html)
Add the supplied HTML to the end of each matched element's inner HTML.
|
java.lang.String |
attr(java.lang.String attributeKey)
Get an attribute value from the first matched element that has the attribute.
|
Elements |
attr(java.lang.String attributeKey,
java.lang.String attributeValue)
Set an attribute on all matched elements.
|
Elements |
before(java.lang.String html)
Insert the supplied HTML before each matched element's outer HTML.
|
Elements |
clone()
Creates a deep copy of these elements.
|
java.util.List<java.lang.String> |
eachAttr(java.lang.String attributeKey)
Get the attribute value for each of the matched elements.
|
java.util.List<java.lang.String> |
eachText()
Get the text content of each of the matched elements.
|
Elements |
empty()
Empty (remove all child nodes from) each matched element.
|
Elements |
eq(int index)
Get the nth matched element as an Elements object.
|
Elements |
filter(NodeFilter nodeFilter)
Perform a depth-first filtering on each of the selected elements.
|
Element |
first()
Get the first matched element.
|
java.util.List<FormElement> |
forms()
Get the
FormElement forms from the selected elements, if any. |
boolean |
hasAttr(java.lang.String attributeKey)
Checks if any of the matched elements have this attribute defined.
|
boolean |
hasClass(java.lang.String className)
Determine if any of the matched elements have this class name set in their
class attribute. |
boolean |
hasText()
Test if any matched ReactElement has any text content, that is not just whitespace.
|
java.lang.String |
html()
Get the combined inner HTML of all matched elements.
|
Elements |
html(java.lang.String html)
Set the inner HTML of each matched element.
|
boolean |
is(java.lang.String query)
Test if any of the matched elements match the supplied query.
|
Element |
last()
Get the last matched element.
|
Elements |
next()
Get the immediate next element sibling of each element in this list.
|
Elements |
next(java.lang.String query)
Get the immediate next element sibling of each element in this list, filtered by the query.
|
Elements |
nextAll()
Get all of the following element siblings of each element in this list.
|
Elements |
nextAll(java.lang.String query)
Get all of the following element siblings of each element in this list, filtered by the query.
|
Elements |
not(java.lang.String query)
Remove elements from this list that match the
Selector query. |
java.lang.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(java.lang.String html)
Add the supplied HTML to the start of each matched element's inner HTML.
|
Elements |
prev()
Get the immediate previous element sibling of each element in this list.
|
Elements |
prev(java.lang.String query)
Get the immediate previous element sibling of each element in this list, filtered by the query.
|
Elements |
prevAll()
Get all of the previous element siblings of each element in this list.
|
Elements |
prevAll(java.lang.String query)
Get all of the previous element siblings of each element in this list, filtered by the query.
|
Elements |
remove()
Remove each matched element from the DOM.
|
Elements |
removeAttr(java.lang.String attributeKey)
Remove an attribute from every matched element.
|
Elements |
removeClass(java.lang.String className)
Remove the class name from every matched element's
class attribute, if present. |
Elements |
select(java.lang.String query)
Find matching elements within this element list.
|
Elements |
tagName(java.lang.String tagName)
Update the tag name of each matched element.
|
java.lang.String |
text()
Get the combined text of all the matched elements.
|
Elements |
toggleClass(java.lang.String className)
Toggle the class name on every matched element's
class attribute. |
java.lang.String |
toString()
Get the combined outer HTML of all matched elements.
|
Elements |
traverse(NodeVisitor nodeVisitor)
Perform a depth-first traversal on each of the selected elements.
|
Elements |
unwrap()
Removes the matched elements from the DOM, and moves their children up into their parents.
|
java.lang.String |
val()
Get the form element's value of the first matched element.
|
Elements |
val(java.lang.String value)
Set the form element's value in each of the matched elements.
|
Elements |
wrap(java.lang.String html)
Wrap the supplied HTML around each matched elements.
|
add, add, addAll, addAll, clear, contains, ensureCapacity, forEach, get, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
public Elements()
public Elements(int initialCapacity)
public Elements(java.util.Collection<Element> elements)
public Elements(java.util.List<Element> elements)
public Elements(Element... elements)
public Elements clone()
clone
in class java.util.ArrayList<Element>
public java.lang.String attr(java.lang.String attributeKey)
attributeKey
- The attribute key.hasAttr(String)
public boolean hasAttr(java.lang.String attributeKey)
attributeKey
- attribute keypublic java.util.List<java.lang.String> eachAttr(java.lang.String attributeKey)
attributeKey
- the attribute name to return values for. You can add the abs:
prefix to the key to
get absolute URLs from relative URLs, e.g.: doc.select("a").eachAttr("abs:href")
.public Elements attr(java.lang.String attributeKey, java.lang.String attributeValue)
attributeKey
- attribute keyattributeValue
- attribute valuepublic Elements removeAttr(java.lang.String attributeKey)
attributeKey
- The attribute to remove.public Elements addClass(java.lang.String className)
class
attribute.className
- class name to addpublic Elements removeClass(java.lang.String className)
class
attribute, if present.className
- class name to removepublic Elements toggleClass(java.lang.String className)
class
attribute.className
- class name to add if missing, or remove if present, from every element.public boolean hasClass(java.lang.String className)
class
attribute.className
- class name to check forpublic java.lang.String val()
Element.val()
public Elements val(java.lang.String value)
value
- The value to set into each matched elementpublic java.lang.String text()
Note that it is possible to get repeats if the matched elements contain both parent elements and their own children, as the ReactElement.text() method returns the combined text of a parent and all its children.
Element.text()
,
eachText()
public boolean hasText()
Element.hasText()
public java.util.List<java.lang.String> eachText()
Element.text()
,
Element.hasText()
,
text()
public java.lang.String html()
text()
,
outerHtml()
public java.lang.String outerHtml()
public java.lang.String toString()
outerHtml()
.public Elements tagName(java.lang.String tagName)
<i>
to a <em>
, do
doc.select("i").tagName("em");
tagName
- the new tag nameElement.tagName(String)
public Elements html(java.lang.String html)
html
- HTML to parse and set into each matched element.Element.html(String)
public Elements prepend(java.lang.String html)
html
- HTML to add inside each element, before the existing HTMLElement.prepend(String)
public Elements append(java.lang.String html)
html
- HTML to add inside each element, after the existing HTMLElement.append(String)
public Elements before(java.lang.String html)
html
- HTML to insert before each elementElement.before(String)
public Elements after(java.lang.String html)
html
- HTML to insert after each elementElement.after(String)
public Elements wrap(java.lang.String 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>
html
- HTML to wrap around each element, e.g. <div class="head"></div>
. Can be arbitrarily deep.Element.wrap(java.lang.String)
public Elements unwrap()
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>
Node.unwrap()
public Elements empty()
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>
Element.empty()
,
remove()
public Elements remove()
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.
Element.empty()
,
empty()
public Elements select(java.lang.String query)
query
- A Selector
querypublic Elements not(java.lang.String query)
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>]
query
- the selector query whose results should be removed from these elementspublic Elements eq(int index)
See also ArrayList.get(int)
to retrieve an ReactElement.
index
- the (zero-based) index of the element in the list to retainpublic boolean is(java.lang.String query)
query
- A selectorpublic Elements next()
public Elements next(java.lang.String query)
query
- CSS query to match siblings againstpublic Elements nextAll()
public Elements nextAll(java.lang.String query)
query
- CSS query to match siblings againstpublic Elements prev()
public Elements prev(java.lang.String query)
query
- CSS query to match siblings againstpublic Elements prevAll()
public Elements prevAll(java.lang.String query)
query
- CSS query to match siblings againstpublic Elements parents()
public Element first()
null
if contents is empty.public Element last()
null
if contents is empty.public Elements traverse(NodeVisitor nodeVisitor)
nodeVisitor
- the visitor callbacks to perform on each nodepublic Elements filter(NodeFilter nodeFilter)
nodeFilter
- the filter callbacks to perform on each nodepublic java.util.List<FormElement> forms()
FormElement
forms from the selected elements, if any.FormElement
s pulled from the matched elements. The list will be empty if the elements contain
no forms.