public class Element extends Node
Constructor and Description |
---|
Element(Tag tag, String baseUri)
Create a new Element from a tag and a base URI.
|
Element(Tag tag, String baseUri, Attributes attributes)
Create a new, standalone Element.
|
Modifier and Type | Method and Description |
---|---|
Element |
addClass(String className)
Add a class name to this element's class attribute.
|
Element |
after(Node node)
Insert the specified node into the DOM after this node (as a following sibling).
|
Element |
after(String html)
Insert the specified HTML into the DOM after this element (as a following sibling).
|
Element |
append(String html)
Add inner HTML to this element.
|
Element |
appendChild(Node child)
Add a node child node to this element.
|
Element |
appendElement(String tagName)
Create a new element by tag name, and add it as the last child.
|
Element |
appendText(String text)
Create and append a new TextNode to this element.
|
Element |
attr(String attributeKey, boolean attributeValue)
Set a boolean attribute value on this element.
|
Element |
attr(String attributeKey, String attributeValue)
Set an attribute value on this element.
|
Element |
before(Node node)
Insert the specified node into the DOM before this node (as a preceding sibling).
|
Element |
before(String html)
Insert the specified HTML into the DOM before this element (as a preceding sibling).
|
Element |
child(int index)
Get a child element of this element, by its 0-based index number.
|
Elements |
children()
Get this element's child elements.
|
String |
className()
Gets the literal value of this element's "class" attribute, which may include multiple class names, space separated.
|
Set<String> |
classNames()
Get all of the element's class names.
|
Element |
classNames(Set<String> classNames)
Set the element's class attribute to the supplied class names.
|
Object |
clone()
Create a stand-alone, deep copy of this node, and all of its children.
|
String |
cssSelector()
Get a CSS selector that will uniquely select this element.
|
String |
data()
Get the combined data of this element.
|
List<DataNode> |
dataNodes()
Get this element's child data nodes.
|
Map<String,String> |
dataset()
Get this element's HTML5 custom data attributes.
|
int |
elementSiblingIndex()
Get the list index of this element in its element sibling list.
|
Element |
empty()
Remove all of the element's child nodes.
|
Element |
firstElementSibling()
Gets the first element sibling of this element.
|
Elements |
getAllElements()
Find all elements under this element (including self, and children of children).
|
Element |
getElementById(String id)
Find an element by ID, including or under this element.
|
Elements |
getElementsByAttribute(String key)
Find elements that have a named attribute set.
|
Elements |
getElementsByAttributeStarting(String keyPrefix)
Find elements that have an attribute name starting with the supplied prefix.
|
Elements |
getElementsByAttributeValue(String key, String value)
Find elements that have an attribute with the specific value.
|
Elements |
getElementsByAttributeValueContaining(String key, String match)
Find elements that have attributes whose value contains the match string.
|
Elements |
getElementsByAttributeValueEnding(String key, String valueSuffix)
Find elements that have attributes that end with the value suffix.
|
Elements |
getElementsByAttributeValueMatching(String key, Pattern pattern)
Find elements that have attributes whose values match the supplied regular expression.
|
Elements |
getElementsByAttributeValueMatching(String key, String regex)
Find elements that have attributes whose values match the supplied regular expression.
|
Elements |
getElementsByAttributeValueNot(String key, String value)
Find elements that either do not have this attribute, or have it with a different value.
|
Elements |
getElementsByAttributeValueStarting(String key, String valuePrefix)
Find elements that have attributes that start with the value prefix.
|
Elements |
getElementsByClass(String className)
Find elements that have this class, including or under this element.
|
Elements |
getElementsByIndexEquals(int index)
Find elements whose sibling index is equal to the supplied index.
|
Elements |
getElementsByIndexGreaterThan(int index)
Find elements whose sibling index is greater than the supplied index.
|
Elements |
getElementsByIndexLessThan(int index)
Find elements whose sibling index is less than the supplied index.
|
Elements |
getElementsByTag(String tagName)
Finds elements, including and recursively under this element, with the specified tag name.
|
Elements |
getElementsContainingOwnText(String searchText)
Find elements that directly contain the specified string.
|
Elements |
getElementsContainingText(String searchText)
Find elements that contain the specified string.
|
Elements |
getElementsMatchingOwnText(Pattern pattern)
Find elements whose own text matches the supplied regular expression.
|
Elements |
getElementsMatchingOwnText(String regex)
Find elements whose text matches the supplied regular expression.
|
Elements |
getElementsMatchingText(Pattern pattern)
Find elements whose text matches the supplied regular expression.
|
Elements |
getElementsMatchingText(String regex)
Find elements whose text matches the supplied regular expression.
|
boolean |
hasClass(String className)
Tests if this element has a class.
|
boolean |
hasText()
Test if this element has any text content (that is not just whitespace).
|
String |
html()
Retrieves the element's inner HTML.
|
Appendable |
html(Appendable appendable)
Write this node and its children to the given Appendable .
|
Element |
html(String html)
Set this element's inner HTML.
|
String |
id()
Get the id attribute of this element.
|
Element |
insertChildren(int index, CollectionNode> children)
Inserts the given child nodes into this element at the specified index.
|
boolean |
isBlock()
Test if this element is a block-level element.
|
Element |
lastElementSibling()
Gets the last element sibling of this element
|
Element |
nextElementSibling()
Gets the next sibling element of this element.
|
String |
nodeName()
Get the node name of this node.
|
String |
ownText()
Gets the text owned by this element only; does not get the combined text of all children.
|
Element |
parent()
Gets this node's parent node.
|
Elements |
parents()
Get this element's parent and ancestors, up to the document root.
|
Element |
prepend(String html)
Add inner HTML into this element.
|
Element |
prependChild(Node child)
Add a node to the start of this element's children.
|
Element |
prependElement(String tagName)
Create a new element by tag name, and add it as the first child.
|
Element |
prependText(String text)
Create and prepend a new TextNode to this element.
|
Element |
previousElementSibling()
Gets the previous element sibling of this element.
|
Element |
removeClass(String className)
Remove a class name from this element's class attribute.
|
Elements |
select(String cssQuery)
Find elements that match the Selector CSS query, with this element as the starting context.
|
Elements |
siblingElements()
Get sibling elements.
|
Tag |
tag()
Get the Tag for this element.
|
String |
tagName()
Get the name of the tag for this element.
|
Element |
tagName(String tagName)
Change the tag of this element.
|
String |
text()
Gets the combined text of this element and all its children.
|
Element |
text(String text)
Set the text of this element.
|
List<TextNode> |
textNodes()
Get this element's child text nodes.
|
Element |
toggleClass(String className)
Toggle a class name on this element's class attribute: if present, remove it; otherwise add it.
|
String |
toString() |
String |
val()
Get the value of a form element (input, textarea, etc).
|
Element |
val(String value)
Set the value of a form element (input, textarea, etc).
|
Element |
wrap(String html)
Wrap the supplied HTML around this element.
|
absUrl, addChildren, addChildren, attr, attributes, baseUri, childNode, childNodes, childNodesAsArray, childNodesCopy, childNodeSize, doClone, ensureChildNodes, equals, hasAttr, hasSameValue, indent, nextSibling, outerHtml, outerHtml, ownerDocument, parentNode, previousSibling, remove, removeAttr, removeChild, reparentChild, replaceChild, replaceWith, setBaseUri, setParentNode, setSiblingIndex, siblingIndex, siblingNodes, traverse, unwrap
public Element(Tag tag, String baseUri, Attributes attributes)
tag
- tag of this element
baseUri
- the base URI
attributes
- initial attributes
appendChild(Node)
, appendElement(String)
public Element(Tag tag, String baseUri)
tag
- element tag
baseUri
- the base URI of this element. It is acceptable for the base URI to be an empty string, but not null.
Tag.valueOf(String)
public String nodeName()
Node
public String tagName()
div
public Element tagName(String tagName)
to a
with el.tagName("div");
.
tagName
- new tag name for this element
public Tag tag()
public boolean isBlock()
== true
or an inline element
== false
).
public String id()
id
attribute of this element.
public Element attr(String attributeKey, String attributeValue)
public Element attr(String attributeKey, boolean attributeValue)
true
sets the attribute value to "" and marks the attribute as boolean so no value is written out. Setting to false
removes the attribute with the same key if it exists.
attributeKey
- the attribute key
attributeValue
- the attribute value
public Map<String,String> dataset()
E.g., the element
...
has the dataset package=jsoup, language=java
.
This map is a filtered view of the element's attribute map. Changes to one map (add, remove, update) are reflected in the other map.
You can find elements that have data attributes using the [^data-]
attribute key prefix selector.
key=value
custom data attributes.
public final Element parent()
Node
public Elements parents()
public Element child(int index)
Note that an element can have both mixed Nodes and Elements as children. This method inspects a filtered list of children that are elements, and the index is based on that filtered list.
index
- the index number of the element to retrieve
IndexOutOfBoundsException
Node.childNode(int)
public Elements children()
This is effectively a filter on Node.childNodes()
to get Element nodes.
Node.childNodes()
public List<TextNode> textNodes()
This is effectively a filter on Node.childNodes()
to get Text nodes.
One Two Three
Four
with the p
element selected:
p.text()
= "One Two Three Four"
p.ownText()
= "One Three Four"
p.children()
= Elements[,
]
p.childNodes()
= List["One ", , " Three ",
, " Four"]
p.textNodes()
= List["One ", " Three ", " Four"]
public List<DataNode> dataNodes()
This is effectively a filter on Node.childNodes()
to get Data nodes.
data()
public Elements select(String cssQuery)
Selector
CSS query, with this element as the starting context. Matched elements may include this element, or any of its children.
This method is generally more powerful to use than the DOM-type getElementBy*
methods, because multiple filters can be combined, e.g.:
el.select("a[href]")
- finds links (a
tags with href
attributes) el.select("a[href*=example.com]")
- finds links pointing to example.com (loosely) See the query syntax documentation in Selector
.
public Element appendChild(Node child)
child
- node to add.
public Element prependChild(Node child)
child
- node to add.
public Element insertChildren(int index, CollectionNode> children)
index
- 0-based index to insert children at. Specify 0
to insert at the start, -1
at the end
children
- child nodes to insert
public Element appendElement(String tagName)
tagName
- the name of the tag (e.g. div
).
parent.appendElement("h1").attr("id", "header").text("Welcome");
public Element prependElement(String tagName)
tagName
- the name of the tag (e.g. div
).
parent.prependElement("h1").attr("id", "header").text("Welcome");
public Element appendText(String text)
text
- the unencoded text to add
public Element prependText(String text)
text
- the unencoded text to add
public Element append(String html)
html
- HTML to add inside this element, after the existing HTML
html(String)
public Element prepend(String html)
html
- HTML to add inside this element, before the existing HTML
html(String)
public Element before(String html)
before
in class Node
html
- HTML to add before this element
after(String)
public Element before(Node node)
before
in class Node
node
- to add before this element
after(Node)
public Element after(String html)
after
in class Node
html
- HTML to add after this element
before(String)
public Element after(Node node)
after
in class Node
node
- to add after this element
before(Node)
public Element empty()
public String cssSelector()
If the element has an ID, returns #id; otherwise returns the parent (if any) CSS selector, followed by '>', followed by a unique selector for the element (tag.class.class:nth-child(n)).
public Elements siblingElements()
public Element nextElementSibling()
div
contains two p
s, the nextElementSibling
of the first p
is the second p
.
This is similar to Node.nextSibling()
, but specifically finds only Elements
previousElementSibling()
public Element previousElementSibling()
nextElementSibling()
public Element firstElementSibling()
public int elementSiblingIndex()
public Element lastElementSibling()
public Elements getElementsByTag(String tagName)
tagName
- The tag name to search for (case insensitively).
public Element getElementById(String id)
Note that this finds the first matching ID, starting with this element. If you search down from a different starting point, it is possible to find a different element by ID. For unique element by ID within a Document, use getElementById(String)
id
- The ID to search for.
public Elements getElementsByClass(String className)
Elements can have multiple classes (e.g.
el.getElementsByClass("header");
.
className
- the name of the class to search for.
hasClass(String)
, classNames()
public Elements getElementsByAttribute(String key)
key
- name of the attribute, e.g. href
public Elements getElementsByAttributeStarting(String keyPrefix)
data-
to find elements that have HTML5 datasets.
keyPrefix
- name prefix of the attribute e.g. data-
public Elements getElementsByAttributeValue(String key, String value)
key
- name of the attribute
value
- value of the attribute
public Elements getElementsByAttributeValueNot(String key, String value)
key
- name of the attribute
value
- value of the attribute
public Elements getElementsByAttributeValueStarting(String key, String valuePrefix)
key
- name of the attribute
valuePrefix
- start of attribute value
public Elements getElementsByAttributeValueEnding(String key, String valueSuffix)
key
- name of the attribute
valueSuffix
- end of the attribute value
public Elements getElementsByAttributeValueContaining(String key, String match)
key
- name of the attribute
match
- substring of value to search for
public Elements getElementsByAttributeValueMatching(String key, Pattern pattern)
key
- name of the attribute
pattern
- compiled regular expression to match against attribute values
public Elements getElementsByAttributeValueMatching(String key, String regex)
key
- name of the attribute
regex
- regular expression to match against attribute values. You can use embedded flags (such as (?i) and (?m) to control regex options.
public Elements getElementsByIndexLessThan(int index)
index
- 0-based index
public Elements getElementsByIndexGreaterThan(int index)
index
- 0-based index
public Elements getElementsByIndexEquals(int index)
index
- 0-based index
public Elements getElementsContainingText(String searchText)
searchText
- to look for in the element's text
text()
public Elements getElementsContainingOwnText(String searchText)
searchText
- to look for in the element's own text
ownText()
public Elements getElementsMatchingText(Pattern pattern)
pattern
- regular expression to match text against
text()
public Elements getElementsMatchingText(String regex)
regex
- regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options.
text()
public Elements getElementsMatchingOwnText(Pattern pattern)
pattern
- regular expression to match text against
ownText()
public Elements getElementsMatchingOwnText(String regex)
regex
- regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options.
ownText()
public Elements getAllElements()
public String text()
For example, given HTML
Hello there now!
p.text()
returns "Hello there now!"
ownText()
, textNodes()
public String ownText()
For example, given HTML
Hello there now!
p.ownText()
returns "Hello now!"
, whereas p.text()
returns "Hello there now!"
. Note that the text within the b
element is not returned, as it is not a direct child of the p
element.
text()
, textNodes()
public Element text(String text)
text
- unencoded text
public boolean hasText()
public String data()
script
tag.
dataNodes()
public String className()
returns, "header gray
")
public Set<String> classNames()
, returns a set of two elements "header", "gray"
. Note that modifications to this set are not pushed to the backing class
attribute; use the classNames(java.util.Set)
method to persist them.
public Element classNames(Set<String> classNames)
class
attribute to the supplied class names.
classNames
- set of classes
public boolean hasClass(String className)
className
- name of class to check for
public Element addClass(String className)
class
attribute.
className
- class name to add
public Element removeClass(String className)
class
attribute.
className
- class name to remove
public Element toggleClass(String className)
class
attribute: if present, remove it; otherwise add it.
className
- class name to toggle
public String val()
public Element val(String value)
value
- value to set
public String html()
Node.outerHtml()
public Appendable html(Appendable appendable)
Appendable
.
html
in class Node
appendable
- the Appendable
to write to.
Appendable
, for chaining.
public Element html(String html)
html
- HTML to parse and set into this element
append(String)
public Object clone()
Node
The cloned node may be adopted into another Document or node structure using appendChild(Node)
.
Copyright © 1998–2019 iText Group NV. All rights reserved.