|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.jsoup.nodes.Node
org.jsoup.nodes.Element
public class Element
A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements). From an Element, you can extract data, traverse the node graph, and manipulate the HTML.
| Constructor Summary | |
|---|---|
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. |
|
| Method Summary | |
|---|---|
Element |
addClass(String className)
Add a class name to this element's class attribute. |
Element |
after(String html)
Insert the specified HTML into the DOM after this element (i.e. |
Element |
append(String html)
Add inner HTML to this element. |
Element |
appendChild(Node child)
Add a node to the last child of 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,
String attributeValue)
Set an attribute value on this element. |
Element |
before(String html)
Insert the specified HTML into the DOM before this element (i.e. |
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. |
String |
data()
Get the combined data of this element. |
Map<String,String> |
dataset()
Get this element's HTML5 custom data attributes. |
Integer |
elementSiblingIndex()
Get the list index of this element in its element sibling list. |
Element |
empty()
Remove all of the element's child nodes. |
boolean |
equals(Object o)
|
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. |
int |
hashCode()
|
boolean |
hasText()
Test if this element has any text content (that is not just whitespace). |
String |
html()
Retrieves the element's inner HTML. |
Element |
html(String html)
Set this element's inner HTML. |
String |
id()
Get the id attribute of this element. |
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 query)
Find elements that match the selector 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. |
String |
text()
Gets the combined text of this element and all its children. |
Element |
text(String text)
Set the text of this element. |
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. |
| Methods inherited from class org.jsoup.nodes.Node |
|---|
absUrl, addChildren, addChildren, attr, attributes, baseUri, childNode, childNodes, childNodesAsArray, hasAttr, indent, nextSibling, outerHtml, outerHtml, ownerDocument, previousSibling, remove, removeAttr, removeChild, replaceChild, replaceWith, setBaseUri, setParentNode, setSiblingIndex, siblingIndex, siblingNodes |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Element(Tag tag,
String baseUri,
Attributes attributes)
tag - tag of this elementbaseUri - the base URIattributes - initial attributesappendChild(Node),
appendElement(String)
public Element(Tag tag,
String baseUri)
tag - element tagbaseUri - the base URI of this element. It is acceptable for the base URI to be an empty
string, but not null.Tag.valueOf(String)| Method Detail |
|---|
public String nodeName()
Node
nodeName in class Nodepublic String tagName()
div
public Tag tag()
public boolean isBlock()
<div> == true or an inline element
<p> == false).
public String id()
id attribute of this element.
public Element attr(String attributeKey,
String attributeValue)
attr in class NodeattributeKey - The attribute key.attributeValue - The attribute value.
public Map<String,String> dataset()
E.g., the element <div data-package="jsoup" data-language="Java" class="group">... 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
parent in class Nodepublic Elements parents()
public Element child(int index)
index - the index number of the element to retrieve
null if absent.Node.childNode(int)public Elements children()
Node.childNodes() to get Element nodes.
Node.childNodes()public Elements select(String query)
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)
Selector.
query - a Selector query
Selectorpublic Element appendChild(Node child)
child - node to add. Must not already have a parent.
public Element prependChild(Node child)
child - node to add. Must not already have a parent.
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)
html - HTML to add before this element
after(String)public Element after(String html)
html - HTML to add after this element
before(String)public Element empty()
public Element wrap(String html)
html - HTML to wrap around this element, e.g. <div class="head"></div>. Can be arbitralily deep.
public Elements siblingElements()
public Element nextElementSibling()
div contains two ps,
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 Integer 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. <div class="header round first">. This method
checks each class, so you can find the above with 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 attributevalue - value of the attribute
public Elements getElementsByAttributeValueNot(String key,
String value)
key - name of the attributevalue - value of the attribute
public Elements getElementsByAttributeValueStarting(String key,
String valuePrefix)
key - name of the attributevaluePrefix - start of attribute value
public Elements getElementsByAttributeValueEnding(String key,
String valueSuffix)
key - name of the attributevalueSuffix - end of the attribute value
public Elements getElementsByAttributeValueContaining(String key,
String match)
key - name of the attributematch - substring of value to search for
public Elements getElementsByAttributeValueMatching(String key,
Pattern pattern)
key - name of the attributepattern - compiled regular expression to match against attribute values
public Elements getElementsByAttributeValueMatching(String key,
String regex)
key - name of the attributeregex - regular expression to match agaisnt 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 <p>Hello <b>there</b> now!</p>, p.text() returns "Hello there now!"
ownText()public String ownText()
For example, given HTML <p>Hello <b>there</b> now!</p>, 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()public Element text(String text)
text - unencoded text
public boolean hasText()
public String data()
script tag.
public String className()
<div class="header gray"> returns, "header gray")
public Set<String> classNames()
<div class="header gray">},
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()
<div> with one empty <p>, would return
<p></p>. (Whereas Node.outerHtml() would return <div><p></p></div>.)
Node.outerHtml()public Element html(String html)
html - HTML to parse and set into this element
append(String)public String toString()
toString in class Nodepublic boolean equals(Object o)
equals in class Nodepublic int hashCode()
hashCode in class Node
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||