Package org.jsoup.nodes
Class TextNode
- java.lang.Object
-
- org.jsoup.nodes.Node
-
- org.jsoup.nodes.TextNode
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
absUrl(String attributeKey)
Get an absolute URL from a URL attribute that may be relative (i.e.String
attr(String attributeKey)
Get an attribute's value by its key.Node
attr(String attributeKey, String attributeValue)
Set an attribute (key=value).Attributes
attributes()
Get all of the element's attributes.static TextNode
createFromEncoded(String encodedText, String baseUri)
Create a new TextNode from HTML encoded (aka escaped) data.String
getWholeText()
Get the (unencoded) text of this text node, including any newlines and spaces present in the original.boolean
hasAttr(String attributeKey)
Test if this element has an attribute.boolean
isBlank()
Test if this text node is blank -- that is, empty or only whitespace (including newlines).String
nodeName()
Get the node name of this node.Node
removeAttr(String attributeKey)
Remove an attribute from this element.TextNode
splitText(int offset)
Split this text node into two nodes at the specified string offset.String
text()
Get the text content of this text node.TextNode
text(String text)
Set the text content of this text node.String
toString()
-
Methods inherited from class org.jsoup.nodes.Node
addChildren, addChildren, after, after, baseUri, before, before, childNode, childNodes, childNodesAsArray, childNodesCopy, childNodeSize, clone, doClone, ensureChildNodes, equals, hasSameValue, html, indent, nextSibling, outerHtml, outerHtml, ownerDocument, parent, parentNode, previousSibling, remove, removeChild, reparentChild, replaceChild, replaceWith, root, setBaseUri, setParentNode, setSiblingIndex, siblingIndex, siblingNodes, traverse, unwrap, wrap
-
-
-
-
Constructor Detail
-
TextNode
public TextNode(String text, String baseUri)
Create a new TextNode representing the supplied (unencoded) text).- Parameters:
text
- raw textbaseUri
- base uri- See Also:
createFromEncoded(String, String)
-
-
Method Detail
-
nodeName
public String nodeName()
Description copied from class:Node
Get the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof).
-
text
public String text()
Get the text content of this text node.- Returns:
- Unencoded, normalised text.
- See Also:
getWholeText()
-
text
public TextNode text(String text)
Set the text content of this text node.- Parameters:
text
- unencoded text- Returns:
- this, for chaining
-
getWholeText
public String getWholeText()
Get the (unencoded) text of this text node, including any newlines and spaces present in the original.- Returns:
- text
-
isBlank
public boolean isBlank()
Test if this text node is blank -- that is, empty or only whitespace (including newlines).- Returns:
- true if this document is empty or only whitespace, false if it contains any text content.
-
splitText
public TextNode splitText(int offset)
Split this text node into two nodes at the specified string offset. After splitting, this node will contain the original text up to the offset, and will have a new text node sibling containing the text after the offset.- Parameters:
offset
- string offset point to split node at.- Returns:
- the newly created text node containing the text after the offset.
-
createFromEncoded
public static TextNode createFromEncoded(String encodedText, String baseUri)
Create a new TextNode from HTML encoded (aka escaped) data.- Parameters:
encodedText
- Text containing encoded HTML (e.g. <)baseUri
- Base uri- Returns:
- TextNode containing unencoded data (e.g. <)
-
attr
public String attr(String attributeKey)
Description copied from class:Node
Get an attribute's value by its key. Case insensitiveTo get an absolute URL from an attribute that may be a relative URL, prefix the key with
E.g.:abs
, which is a shortcut to theNode.absUrl(java.lang.String)
method.String url = a.attr("abs:href");
- Overrides:
attr
in classNode
- Parameters:
attributeKey
- The attribute key.- Returns:
- The attribute, or empty string if not present (to avoid nulls).
- See Also:
Node.attributes()
,Node.hasAttr(String)
,Node.absUrl(String)
-
attributes
public Attributes attributes()
Description copied from class:Node
Get all of the element's attributes.- Overrides:
attributes
in classNode
- Returns:
- attributes (which implements iterable, in same order as presented in original HTML).
-
attr
public Node attr(String attributeKey, String attributeValue)
Description copied from class:Node
Set an attribute (key=value). If the attribute already exists, it is replaced.
-
hasAttr
public boolean hasAttr(String attributeKey)
Description copied from class:Node
Test if this element has an attribute. Case insensitive
-
removeAttr
public Node removeAttr(String attributeKey)
Description copied from class:Node
Remove an attribute from this element.- Overrides:
removeAttr
in classNode
- Parameters:
attributeKey
- The attribute to remove.- Returns:
- this (for chaining)
-
absUrl
public String absUrl(String attributeKey)
Description copied from class:Node
Get an absolute URL from a URL attribute that may be relative (i.e. an<a href>
or<img src>
).E.g.:
String absUrl = linkEl.absUrl("href");
If the attribute value is already absolute (i.e. it starts with a protocol, like
http://
orhttps://
etc), and it successfully parses as a URL, the attribute is returned directly. Otherwise, it is treated as a URL relative to the element'sNode.baseUri
, and made absolute using that.As an alternate, you can use the
Node.attr(java.lang.String)
method with theabs:
prefix, e.g.:String absUrl = linkEl.attr("abs:href");
- Overrides:
absUrl
in classNode
- Parameters:
attributeKey
- The attribute key- Returns:
- An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or could not be made successfully into a URL.
- See Also:
Node.attr(java.lang.String)
,URL(java.net.URL, String)
-
-