|
||||||||||
| 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 |
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 |
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. |
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 |
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 |
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 |
getElementsByTag(String tagName)
Finds elements, including and recursively under this element, with the specified tag name. |
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. |
Element |
parent()
Gets this node's parent node. |
Element |
prepend(String html)
Add inner HTML to 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()
|
Element |
wrap(String html)
Wrap the supplied HTML around this element. |
| Methods inherited from class org.jsoup.nodes.Node |
|---|
absUrl, addChild, attr, attributes, baseUri, childNode, childNodes, hasAttr, indent, indexInList, nextSibling, nodeDepth, outerHtml, previousSibling, removeAttr, removeChild, replaceChild, setBaseUri, setParentNode, 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 Element parent()
Node
parent in class Nodepublic 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 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
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 getAllElements()
public String 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 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 | |||||||||