public class

TextNode

extends Node
java.lang.Object
   ↳ org.jsoup.nodes.Node
     ↳ org.jsoup.nodes.TextNode

Class Overview

A text node.

Summary

Public Constructors
TextNode(String text, String baseUri)
Create a new TextNode representing the supplied (unencoded) text).
Public Methods
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.
TextNode text(String text)
Set the text content of this text node.
String text()
Get the text content of this text node.
String toString()
[Expand]
Inherited Methods
From class org.jsoup.nodes.Node
From class java.lang.Object

Public Constructors

public TextNode (String text, String baseUri)

Create a new TextNode representing the supplied (unencoded) text).

Parameters
text raw text
baseUri base uri

Public Methods

public String absUrl (String attributeKey)

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:// or https:// etc), and it successfully parses as a URL, the attribute is returned directly. Otherwise, it is treated as a URL relative to the element's baseUri(), and made absolute using that.

As an alternate, you can use the attr(String) method with the abs: prefix, e.g.: String absUrl = linkEl.attr("abs:href");

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.

public String attr (String attributeKey)

Get an attribute's value by its key.

To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs, which is a shortcut to the absUrl(String) method. E.g.:

String url = a.attr("abs:href");

Parameters
attributeKey The attribute key.
Returns
  • The attribute, or empty string if not present (to avoid nulls).

public Node attr (String attributeKey, String attributeValue)

Set an attribute (key=value). If the attribute already exists, it is replaced.

Parameters
attributeKey The attribute key.
attributeValue The attribute value.
Returns
  • this (for chaining)

public Attributes attributes ()

Get all of the element's attributes.

Returns
  • attributes (which implements iterable, in same order as presented in original HTML).

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. &lt;)
Returns
  • TextNode containing unencoded data (e.g. <)

public String getWholeText ()

Get the (unencoded) text of this text node, including any newlines and spaces present in the original.

Returns
  • text

public boolean hasAttr (String attributeKey)

Test if this element has an attribute.

Parameters
attributeKey The attribute key to check.
Returns
  • true if the attribute exists, false if not.

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.

public String nodeName ()

Get the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof).

Returns
  • node name

public Node removeAttr (String attributeKey)

Remove an attribute from this element.

Parameters
attributeKey The attribute to remove.
Returns
  • this (for chaining)

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.

public TextNode text (String text)

Set the text content of this text node.

Parameters
text unencoded text
Returns
  • this, for chaining

public String text ()

Get the text content of this text node.

Returns
  • Unencoded, normalised text.
See Also

public String toString ()