public class Elements extends 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.
modCount
Constructor and Description |
---|
Elements() |
Elements(Collection<Element> elements) |
Elements(Element... elements) |
Elements(int initialCapacity) |
Elements(List<Element> elements) |
Modifier and Type | Method and Description |
---|---|
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.
|
Object |
clone()
Creates a deep copy of these 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.
|
Element |
first()
Get the first matched element.
|
List<FormElement> |
forms()
Get the FormElement forms from the selected elements, if any.
|
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() |
String |
html()
Get the combined inner HTML of all matched elements.
|
Elements |
html(String html)
Set the inner HTML of each matched element.
|
boolean |
is(String query)
Test if any of the matched elements match the supplied query.
|
Element |
last()
Get the last matched element.
|
Elements |
not(String query)
Remove elements from this list that 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.
|
Elements |
remove()
Remove each matched element from the DOM.
|
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.
|
Elements |
select(String query)
Find matching elements within this element list.
|
Elements |
tagName(String tagName)
Update the tag name of each matched element.
|
String |
text()
Get the combined text of all the matched elements.
|
Elements |
toggleClass(String className)
Toggle the class name on every matched element's class attribute.
|
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.
|
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.
|
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
equals, hashCode
containsAll
finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode
parallelStream, stream
public Elements()
public Elements(int initialCapacity)
public Elements(Collection<Element> elements)
public Elements(Element... elements)
public Object clone()
public String attr(String attributeKey)
attributeKey
- The attribute key.
hasAttr(String)
public boolean hasAttr(String attributeKey)
attributeKey
- attribute key
public Elements attr(String attributeKey, String attributeValue)
attributeKey
- attribute key
attributeValue
- attribute value
public Elements removeAttr(String attributeKey)
attributeKey
- The attribute to remove.
public Elements addClass(String className)
class
attribute.
className
- class name to add
public Elements removeClass(String className)
class
attribute, if present.
className
- class name to remove
public Elements toggleClass(String className)
class
attribute.
className
- class name to add if missing, or remove if present, from every element.
public boolean hasClass(String className)
class
attribute.
className
- class name to check for
public String val()
Element.val()
public Elements val(String value)
value
- The value to set into each matched element
public String text()
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.
Element.text()
public boolean hasText()
public String html()
text()
, outerHtml()
public String outerHtml()
public String toString()
outerHtml()
.
toString
in class AbstractCollection<Element>
text()
, html()
public Elements tagName(String tagName)
to a
, do doc.select("i").tagName("em");
tagName
- the new tag name
Element.tagName(String)
public Elements html(String html)
html
- HTML to parse and set into each matched element.
Element.html(String)
public Elements prepend(String html)
html
- HTML to add inside each element, before the existing HTML
Element.prepend(String)
public Elements append(String html)
html
- HTML to add inside each element, after the existing HTML
Element.append(String)
public Elements before(String html)
html
- HTML to insert before each element
Element.before(String)
public Elements after(String html)
html
- HTML to insert after each element
Element.after(String)
public Elements wrap(String html)
This is Jsoup
, doc.select("b").wrap("");
becomes This is jsoup
html
- HTML to wrap around each element, e.g.
. 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:doc.select("font").unwrap();
HTML =
Node.unwrap()
public Elements empty()
E.g. HTML: Hello there now
doc.select("p").empty();
HTML =
Element.empty()
, remove()
public Elements remove()
E.g. HTML: Hello there
doc.select("p").remove();
HTML =
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(String query)
query
- A Selector
query
public Elements not(String query)
Selector
query.
E.g. HTML:
Elements divs = doc.select("div").not(".logo");
Result: divs: [
query
- the selector query whose results should be removed from these elements
public Elements eq(int index)
See also ArrayList.get(int)
to retrieve an Element.
index
- the (zero-based) index of the element in the list to retain
public boolean is(String query)
query
- A selector
public 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 node
public 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.
Copyright © 1998–2019 iText Group NV. All rights reserved.