org.jsoup.nodes
Class Node

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

public abstract class Node
extends Object

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

Author:
Jonathan Hedley, jonathan@hedley.net

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)
           
 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 childNode(int index)
          Get a child node by index
 List<Node> childNodes()
          Get this node's children.
protected  Node[] childNodesAsArray()
           
 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)
           
 Integer siblingIndex()
          Get the list index of this node in its node sibling list.
 List<Node> siblingNodes()
          Retrieves this node's sibling nodes.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, 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>.

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.

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.


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 Integer 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


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