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(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 addChild(Node in)
           
 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.
 boolean equals(Object o)
           
 boolean hasAttr(String attributeKey)
          Test if this element has an attribute.
 int hashCode()
           
protected  void indent(StringBuilder accum)
           
protected static
<N extends Node>
Integer
indexInList(N search, List<N> nodes)
           
 Node nextSibling()
          Get this node's next sibling.
protected  int nodeDepth()
           
abstract  String nodeName()
          Get the node name of this node.
 String outerHtml()
          Get the outer HTML of this node.
 Node parent()
          Gets this node's parent node.
 Node previousSibling()
          Get this node's previous sibling.
 Node removeAttr(String attributeKey)
          Remove an attribute from this element.
protected  void removeChild(Node out)
           
protected  void replaceChild(Node out, Node in)
           
 void setBaseUri(String baseUri)
          Update the base URI of this node.
protected  void setParentNode(Node parentNode)
           
 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)
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.

parent

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

Returns:
parent node; or null if no parent.

setParentNode

protected void setParentNode(Node parentNode)

replaceChild

protected void replaceChild(Node out,
                            Node in)

removeChild

protected void removeChild(Node out)

addChild

protected void addChild(Node in)

nodeDepth

protected int nodeDepth()

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

indexInList

protected static <N extends Node> Integer indexInList(N search,
                                                      List<N> nodes)

outerHtml

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

Returns:
HTML

toString

public String toString()
Overrides:
toString in class Object

indent

protected void indent(StringBuilder accum)

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.