org.jsoup.nodes
Class Node

java.lang.Object
  extended by org.jsoup.nodes.Node
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
Comment, DataNode, DocumentType, Element, TextNode, XmlDeclaration

public abstract class Node
extends Object
implements Cloneable

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

Author:
Jonathan Hedley, [email protected]

Constructor Summary
protected Node()
          Default constructor.
protected Node(String baseUri)
           
protected Node(String baseUri, Attributes attributes)
          Create a new Node.
 
Method Summary
 String absUrl(String attributeKey)
          Get an absolute URL from a URL attribute that may be relative (i.e.
protected  void addChildren(int index, Node... children)
           
protected  void addChildren(Node... children)
           
 Node after(Node node)
          Insert the specified node into the DOM after this node (i.e.
 Node after(String html)
          Insert the specified HTML into the DOM after this node (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.
 String baseUri()
          Get the base URI of this node.
 Node before(Node node)
          Insert the specified node into the DOM before this node (i.e.
 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.
protected  Node[] childNodesAsArray()
           
 Node clone()
          Create a stand-alone, deep copy of this node, and all of its children.
protected  Node doClone(Node parent)
           
 boolean equals(Object o)
           
 boolean hasAttr(String attributeKey)
          Test if this element has an attribute.
 int hashCode()
           
protected  void indent(StringBuilder accum, int depth, Document.OutputSettings out)
           
 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.
protected  void outerHtml(StringBuilder accum)
           
 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.
protected  void removeChild(Node out)
           
protected  void replaceChild(Node out, Node in)
           
 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.
protected  void setParentNode(Node parentNode)
           
protected  void setSiblingIndex(int siblingIndex)
           
 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 unwrap()
          Removes this node from the DOM, and moves its children up into the node's parent.
 Node wrap(String html)
          Wrap the supplied HTML around this node.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Node

protected Node(String baseUri,
               Attributes attributes)
Create a new Node.

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

Node

protected Node(String baseUri)

Node

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

Method Detail

nodeName

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

attr

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(java.lang.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).
See Also:
attributes(), hasAttr(String), absUrl(String)

attributes

public Attributes attributes()
Get all of the element's attributes.

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

attr

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)

hasAttr

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.

removeAttr

public Node removeAttr(String attributeKey)
Remove an attribute from this element.

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

baseUri

public String baseUri()
Get the base URI of this node.

Returns:
base URI

setBaseUri

public void setBaseUri(String baseUri)
Update the base URI of this node.

Parameters:
baseUri - base URI to set

absUrl

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(java.lang.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.
See Also:
attr(java.lang.String), URL.URL(java.net.URL, String)

childNode

public Node childNode(int index)
Get a child node by index

Parameters:
index - index of child node
Returns:
the child node at this index.

childNodes

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.

childNodesAsArray

protected Node[] childNodesAsArray()

parent

public Node parent()
Gets this node's parent node.

Returns:
parent node; or null if no parent.

ownerDocument

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.

remove

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


before

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 node
Returns:
this node, for chaining
See Also:
after(String)

before

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

Parameters:
node - to add before this node
Returns:
this node, for chaining
See Also:
after(Node)

after

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 node
Returns:
this node, for chaining
See Also:
before(String)

after

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

Parameters:
node - to add after this node
Returns:
this node, for chaining
See Also:
before(Node)

wrap

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.

unwrap

public Node unwrap()
Removes this node from the DOM, and moves its children up into the node's parent. This has the effect of dropping the node but keeping its children.

For example, with the input html:
<div>One <span>Two <b>Three</b></span></div>
Calling element.unwrap() on the span element will result in the html:
<div>One Two <b>Three</b></div>
and the "Two " TextNode being returned.

Returns:
the first child of this node, after the node has been unwrapped. Null if the node had no children.
See Also:
remove(), wrap(String)

replaceWith

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.

setParentNode

protected void setParentNode(Node parentNode)

replaceChild

protected void replaceChild(Node out,
                            Node in)

removeChild

protected void removeChild(Node out)

addChildren

protected void addChildren(Node... children)

addChildren

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

siblingNodes

public List<Node> siblingNodes()
Retrieves this node's sibling nodes. Effectively, node.parent.childNodes().

Returns:
node siblings, including this node

nextSibling

public Node nextSibling()
Get this node's next sibling.

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

previousSibling

public Node previousSibling()
Get this node's previous sibling.

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

siblingIndex

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
See Also:
Element.elementSiblingIndex()

setSiblingIndex

protected void setSiblingIndex(int siblingIndex)

outerHtml

public String outerHtml()
Get the outer HTML of this node.

Returns:
HTML

outerHtml

protected void outerHtml(StringBuilder accum)

toString

public String toString()
Overrides:
toString in class Object

indent

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

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

clone

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 Element.appendChild(Node).

Overrides:
clone in class Object
Returns:
stand-alone cloned node

doClone

protected Node doClone(Node parent)


Copyright © 2009-2011 Jonathan Hedley. All Rights Reserved.