public abstract class

Node

extends Object
implements Cloneable
java.lang.Object
   ↳ org.jsoup.nodes.Node
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

The base, abstract Node model. Elements, Documents, Comments etc are all Node instances.

Summary

Protected Constructors
Node(String baseUri, Attributes attributes)
Create a new Node.
Node(String baseUri)
Node()
Default constructor.
Public Methods
String absUrl(String attributeKey)
Get an absolute URL from a URL attribute that may be relative (i.e.
Node after(String html)
Insert the specified HTML into the DOM after this node (i.e.
Node attr(String attributeKey, String attributeValue)
Set an attribute (key=value).
String attr(String attributeKey)
Get an attribute's value by its key.
Attributes attributes()
Get all of the element's attributes.
String baseUri()
Get the base URI of this node.
Node before(String html)
Insert the specified HTML into the DOM before this node (i.e.
Node childNode(int index)
Get a child node by index
List<Node> childNodes()
Get this node's children.
Node clone()
Create a stand-alone, deep copy of this node, and all of its children.
boolean equals(Object o)
boolean hasAttr(String attributeKey)
Test if this element has an attribute.
int hashCode()
Node nextSibling()
Get this node's next sibling.
abstract String nodeName()
Get the node name of this node.
String outerHtml()
Get the outer HTML of this node.
Document ownerDocument()
Gets the Document associated with this Node.
Node parent()
Gets this node's parent node.
Node previousSibling()
Get this node's previous sibling.
void remove()
Remove (delete) this node from the DOM tree.
Node removeAttr(String attributeKey)
Remove an attribute from this element.
void replaceWith(Node in)
Replace this node in the DOM with the supplied node.
void setBaseUri(String baseUri)
Update the base URI of this node.
int siblingIndex()
Get the list index of this node in its node sibling list.
List<Node> siblingNodes()
Retrieves this node's sibling nodes.
String toString()
Node wrap(String html)
Wrap the supplied HTML around this node.
Protected Methods
void addChildren(Node... children)
void addChildren(int index, Node... children)
Node[] childNodesAsArray()
Node doClone(Node parent)
void indent(StringBuilder accum, int depth, Document.OutputSettings out)
void outerHtml(StringBuilder accum)
void removeChild(Node out)
void replaceChild(Node out, Node in)
void setParentNode(Node parentNode)
void setSiblingIndex(int siblingIndex)
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

protected Node (String baseUri, Attributes attributes)

Create a new Node.

Parameters
baseUri base URI
attributes attributes (not null, but may be empty)

protected Node (String baseUri)

protected Node ()

Default constructor. Doesn't setup base uri, children, or attributes; use with caution.

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 Node after (String html)

Insert the specified HTML into the DOM after this node (i.e. as a following sibling).

Parameters
html HTML to add after this element
Returns
  • this node, for chaining
See Also

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 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 Attributes attributes ()

Get all of the element's attributes.

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

public String baseUri ()

Get the base URI of this node.

Returns
  • base URI

public Node before (String html)

Insert the specified HTML into the DOM before this node (i.e. as a preceeding sibling).

Parameters
html HTML to add before this element
Returns
  • this node, for chaining
See Also

public Node childNode (int index)

Get a child node by index

Parameters
index index of child node
Returns
  • the child node at this index.

public List<Node> childNodes ()

Get this node's children. Presented as an unmodifiable list: new children can not be added, but the child nodes themselves can be manipulated.

Returns
  • list of children. If no children, returns an empty list.

public Node clone ()

Create a stand-alone, deep copy of this node, and all of its children. The cloned node will have no siblings or parent node. As a stand-alone object, any changes made to the clone or any of its children will not impact the original node.

The cloned node may be adopted into another Document or node structure using appendChild(Node).

Returns
  • stand-alone cloned node

public boolean equals (Object o)

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 int hashCode ()

public Node nextSibling ()

Get this node's next sibling.

Returns
  • next sibling, or null if this is the last sibling

public abstract 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 String outerHtml ()

Get the outer HTML of this node.

Returns
  • HTML

public Document ownerDocument ()

Gets the Document associated with this Node.

Returns
  • the Document associated with this Node, or null if there is no such Document.

public Node parent ()

Gets this node's parent node.

Returns
  • parent node; or null if no parent.

public Node previousSibling ()

Get this node's previous sibling.

Returns
  • the previous sibling, or null if this is the first sibling

public void remove ()

Remove (delete) this node from the DOM tree. If this node has children, they are also removed.

public Node removeAttr (String attributeKey)

Remove an attribute from this element.

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

public void replaceWith (Node in)

Replace this node in the DOM with the supplied node.

Parameters
in the node that will will replace the existing node.

public void setBaseUri (String baseUri)

Update the base URI of this node.

Parameters
baseUri base URI to set

public int siblingIndex ()

Get the list index of this node in its node sibling list. I.e. if this is the first node sibling, returns 0.

Returns
  • position in node sibling list

public List<Node> siblingNodes ()

Retrieves this node's sibling nodes. Effectively, node.parent.childNodes().

Returns
  • node siblings, including this node

public String toString ()

public Node wrap (String html)

Wrap the supplied HTML around this node.

Parameters
html HTML to wrap around this element, e.g. <div class="head"></div>. Can be arbitrarily deep.
Returns
  • this node, for chaining.

Protected Methods

protected void addChildren (Node... children)

protected void addChildren (int index, Node... children)

protected Node[] childNodesAsArray ()

protected Node doClone (Node parent)

protected void indent (StringBuilder accum, int depth, Document.OutputSettings out)

protected void outerHtml (StringBuilder accum)

protected void removeChild (Node out)

protected void replaceChild (Node out, Node in)

protected void setParentNode (Node parentNode)

protected void setSiblingIndex (int siblingIndex)