Class Node
- All Implemented Interfaces:
-
Cloneable
- Author:
- Jonathan Hedley, jonathan@hedley.net
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionStringabsUrl(String attributeKey) Get an absolute URL from a URL attribute that may be relative (such as an<a href>or<img src>).protected voidaddChildren(int index, Node... children) protected voidaddChildren(Node... children) after(String html) Insert the specified HTML into the DOM after this node (as a following sibling).Insert the specified node into the DOM after this node (as a following sibling).Stringattr(String attributeKey) Get an attribute's value by its key.attr(String attributeKey, String attributeValue) Set an attribute (key=value).abstract AttributesGet each of the Element's attributes.intGet the number of attributes that this Node has.abstract StringbaseUri()Get the base URI that applies to this node.before(String html) Insert the specified HTML into the DOM before this node (as a preceding sibling).Insert the specified node into the DOM before this node (as a preceding sibling).childNode(int index) Get a child node by its 0-based index.List<Node> Get this node's children.protected Node[]List<Node> Returns a deep copy of this node's children.abstract intGet the number of child nodes that this node holds.Clear (remove) each of the attributes in this node.clone()Create a stand-alone, deep copy of this node, and all of its children.protected Nodeprotected abstract voiddoSetBaseUri(String baseUri) Set the baseUri for just this node (not its descendants), if this Node tracks base URIs.abstract Nodeempty()Delete all this node's children.protected abstract List<Node> booleanequals(@Nullable Object o) Check if this node is the same instance of another (object identity test).filter(NodeFilter nodeFilter) Perform a depth-first controllable traversal through this node and its descendants.@Nullable NodeGets the first child node of this node, ornullif there is none.Gets the first sibling of this node.forEachNode(Consumer<? super Node> action) Perform the supplied action on this Node and each of its descendants, during a depth-first traversal.booleanhasAttr(String attributeKey) Test if this Node has an attribute.protected abstract booleanCheck if this Node has an actual Attributes object.inthashCode()Provides a hashCode for this Node, based on its object identity.booleanChecks if this node has a parent.booleanhasSameValue(@Nullable Object o) Check if this node has the same content as another node.<T extends Appendable>
Thtml(T appendable) Write this node and its children to the givenAppendable.protected voidindent(Appendable accum, int depth, Document.OutputSettings out) Deprecated.internal method moved into Printer; will be removed in a future version@Nullable NodeGets the last child node of this node, ornullif there is none.Gets the last sibling of this node.booleannameIs(String normalName) Test if this node has the specified normalized name, in any namespace.@Nullable ElementGets the next sibling Element of this node.@Nullable NodeGet this node's next sibling.abstract StringnodeName()Get the node name of this node.Stream<Node> Returns a Stream of this Node and all of its descendant Nodes.<T extends Node>
Stream<T> nodeStream(Class<T> type) Returns a Stream of this and descendant nodes, containing only nodes of the specified type.StringGet the node's value.StringGet the normalized name of this node.StringGet the outer HTML of this node.protected voidouterHtml(Appendable accum) protected voidouterHtml(QuietAppendable accum) @Nullable DocumentGets the Document associated with this Node.@Nullable Nodeparent()Gets this node's parent node.@Nullable ElementGets this node's parent Element.booleanparentElementIs(String normalName, String namespace) Test if this node's parent is an Element with the specified normalized name and namespace.booleanparentNameIs(String normalName) Test if this node's parent has the specified normalized name.final @Nullable NodeGets this node's parent node.@Nullable ElementGets the previous Element sibling of this node.@Nullable NodeGet this node's previous sibling.voidremove()Remove (delete) this node from the DOM tree.removeAttr(String attributeKey) Remove an attribute from this node.protected voidremoveChild(Node out) protected voidreparentChild(Node child) protected voidreplaceChild(Node out, Node in) voidreplaceWith(Node in) Replace this node in the DOM with the supplied node.root()Get this node's root node; that is, its topmost ancestor.voidsetBaseUri(String baseUri) Update the base URI of this node and all of its descendants.protected voidsetParentNode(Node parentNode) protected voidsetSiblingIndex(int siblingIndex) Create a stand-alone, shallow copy of this node.intGet the list index of this node in its node sibling list.List<Node> Retrieves this node's sibling nodes.Get the source range (start and end positions) in the original input source from which this node was parsed.StringtoString()Gets this node's outer HTML.traverse(NodeVisitor nodeVisitor) Perform a depth-first traversal through this node and its descendants.@Nullable Nodeunwrap()Removes this node from the DOM, and moves its children up into the node's parent.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 Details
-
Method Details
-
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
-
normalName
public String normalName()Get the normalized name of this node. For node types other than Element, this is the same asnodeName(. For an Element, will be the lower-cased tag name.) - Returns:
- normalized node name
- Since:
- 1.15.4.
-
nodeValue
public String nodeValue()Get the node's value. For a TextNode, the whole text; for a Comment, the comment data; for an Element, wholeOwnText. Returns "" if there is no value.- Returns:
- the node's value
-
nameIs
public boolean nameIs(String normalName) Test if this node has the specified normalized name, in any namespace.- Parameters:
-
normalName- a normalized element name (e.g.div). - Returns:
- true if the element's normal name matches exactly
- Since:
- 1.17.2
-
parentNameIs
public boolean parentNameIs(String normalName) Test if this node's parent has the specified normalized name.- Parameters:
-
normalName- a normalized name (e.g.div). - Returns:
- true if the parent element's normal name matches exactly
- Since:
- 1.17.2
-
parentElementIs
public boolean parentElementIs(String normalName, String namespace) Test if this node's parent is an Element with the specified normalized name and namespace.- Parameters:
-
normalName- a normalized element name (e.g.div). -
namespace- the namespace - Returns:
- true if the parent element's normal name matches exactly, and that element is in the specified namespace
- Since:
- 1.17.2
-
hasAttributes
protected abstract boolean hasAttributes()Check if this Node has an actual Attributes object. -
hasParent
public boolean hasParent()Checks if this node has a parent. Nodes won't have parents if (e.g.) they are newly created and not added as a child to an existing node, or if they are ashallowClone(. In such cases,) parent(will return) null.- Returns:
- if this node has a parent.
-
attr
public String attr(String attributeKey) Get an attribute's value by its key. Case insensitiveTo get an absolute URL from an attribute that may be a relative URL, prefix the key with
E.g.:abs:, which is a shortcut to theabsUrl(method.java.lang.String) 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
Get each of the Element's attributes.- Returns:
- attributes (which implements Iterable, with the same order as presented in the original HTML).
-
attributesSize
public int attributesSize()Get the number of attributes that this Node has.- Returns:
- the number of attributes
- Since:
- 1.14.2
-
attr
Set an attribute (key=value). If the attribute already exists, it is replaced. The attribute key comparison is case insensitive. The key will be set with case sensitivity as set in the parser settings.- Parameters:
-
attributeKey- The attribute key. -
attributeValue- The attribute value. - Returns:
- this (for chaining)
-
hasAttr
public boolean hasAttr(String attributeKey) Test if this Node has an attribute. Case insensitive.- Parameters:
-
attributeKey- The attribute key to check. - Returns:
- true if the attribute exists, false if not.
-
removeAttr
Remove an attribute from this node.- Parameters:
-
attributeKey- The attribute to remove. - Returns:
- this (for chaining)
-
clearAttributes
Clear (remove) each of the attributes in this node.- Returns:
- this, for chaining
-
baseUri
public abstract String baseUri()Get the base URI that applies to this node. Will return an empty string if not defined. Used to make relative links absolute.- Returns:
- base URI
- See Also:
-
doSetBaseUri
protected abstract void doSetBaseUri(String baseUri) Set the baseUri for just this node (not its descendants), if this Node tracks base URIs.- Parameters:
-
baseUri- new URI
-
setBaseUri
public void setBaseUri(String baseUri) Update the base URI of this node and all of its descendants.- Parameters:
-
baseUri- base URI to set
-
absUrl
public String absUrl(String attributeKey) Get an absolute URL from a URL attribute that may be relative (such as 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://orhttps://etc), and it successfully parses as a URL, the attribute is returned directly. Otherwise, it is treated as a URL relative to the element'sbaseUri(, and made absolute using that.) As an alternate, you can use the
attr(method with thejava.lang.String) 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:
-
ensureChildNodes
-
childNode
Get a child node by its 0-based index.- Parameters:
-
index- index of child node - Returns:
- the child node at this index.
- Throws:
-
IndexOutOfBoundsException- if the index is out of bounds.
-
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.
-
childNodesCopy
Returns a deep copy of this node's children. Changes made to these nodes will not be reflected in the original nodes- Returns:
- a deep copy of this node's children
-
childNodeSize
public abstract int childNodeSize()Get the number of child nodes that this node holds.- Returns:
- the number of child nodes that this node holds.
-
childNodesAsArray
-
empty
Delete all this node's children.- Returns:
- this node, for chaining
-
parent
Gets this node's parent node. This is always an Element.- Returns:
- parent node; or null if no parent.
- See Also:
-
parentElement
Gets this node's parent Element.- Returns:
- parent element; or null if this node has no parent.
- Since:
- 1.21.1
- See Also:
-
parentNode
Gets this node's parent node. Not overridable by extending classes, so useful if you really just need the Node type.- Returns:
- parent node; or null if no parent.
-
root
Get this node's root node; that is, its topmost ancestor. If this node is the top ancestor, returnsthis.- Returns:
- topmost ancestor.
-
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. If this node is an orphan, nothing happens. -
before
Insert the specified HTML into the DOM before this node (as a preceding sibling).- Parameters:
-
html- HTML to add before this node - Returns:
- this node, for chaining
- See Also:
-
before
Insert the specified node into the DOM before this node (as a preceding sibling).- Parameters:
-
node- to add before this node - Returns:
- this node, for chaining
- See Also:
-
after
Insert the specified HTML into the DOM after this node (as a following sibling).- Parameters:
-
html- HTML to add after this node - Returns:
- this node, for chaining
- See Also:
-
after
Insert the specified node into the DOM after this node (as a following sibling).- Parameters:
-
node- to add after this node - Returns:
- this node, for chaining
- See Also:
-
wrap
Wrap the supplied HTML around this node.- Parameters:
-
html- HTML to wrap around this node, e.g.<div class="head"></div>. Can be arbitrarily deep. If the input HTML does not parse to a result starting with an Element, this will be a no-op. - Returns:
- this node, for chaining.
-
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:
Calling<div>One <span>Two <b>Three</b></span></div>element.unwrap(on the) spanelement will result in the html:
and the<div>One Two <b>Three</b></div>"Two "TextNodebeing returned.- Returns:
- the first child of this node, after the node has been unwrapped. @{code Null} if the node had no children.
- See Also:
-
replaceWith
Replace this node in the DOM with the supplied node.- Parameters:
-
in- the node that will replace the existing node.
-
setParentNode
-
replaceChild
-
removeChild
-
addChildren
-
addChildren
-
reparentChild
-
siblingNodes
Retrieves this node's sibling nodes. Similar tonode.parent.childNodes(, but does not include this node (a node is not a sibling of itself).) - Returns:
- node siblings. If the node has no parent, returns an empty list.
-
nextSibling
Get this node's next sibling.- Returns:
-
next sibling, or
nullif this is the last sibling
-
previousSibling
Get this node's previous sibling.- Returns:
- the previous sibling, or @{code null} if this is the first sibling
-
siblingIndex
public int siblingIndex()Get the list index of this node in its node sibling list. E.g. if this is the first node sibling, returns 0.- Returns:
- position in node sibling list
- See Also:
-
setSiblingIndex
protected void setSiblingIndex(int siblingIndex) -
firstChild
Gets the first child node of this node, ornullif there is none. This could be any Node type, such as an Element, TextNode, Comment, etc. UseElement.firstElementChild(to get the first Element child.) - Returns:
- the first child node, or null if there are no children.
- Since:
- 1.15.2
- See Also:
-
lastChild
Gets the last child node of this node, ornullif there is none.- Returns:
- the last child node, or null if there are no children.
- Since:
- 1.15.2
- See Also:
-
firstSibling
Gets the first sibling of this node. That may be this node.- Returns:
- the first sibling node
- Since:
- 1.21.1
-
lastSibling
Gets the last sibling of this node. That may be this node.- Returns:
- the last sibling (aka the parent's last child)
- Since:
- 1.21.1
-
nextElementSibling
Gets the next sibling Element of this node. E.g., if adivcontains twops, thenextElementSiblingof the firstpis the secondp.This is similar to
nextSibling(, but specifically finds only Elements.) - Returns:
- the next element, or null if there is no next element
- See Also:
-
previousElementSibling
Gets the previous Element sibling of this node.- Returns:
- the previous element, or null if there is no previous element
- See Also:
-
traverse
Perform a depth-first traversal through this node and its descendants.- Parameters:
-
nodeVisitor- the visitor callbacks to perform on each node - Returns:
- this node, for chaining
-
forEachNode
Perform the supplied action on this Node and each of its descendants, during a depth-first traversal. Nodes may be inspected, changed, added, replaced, or removed.- Parameters:
-
action- the function to perform on the node - Returns:
- this Node, for chaining
- See Also:
-
filter
Perform a depth-first controllable traversal through this node and its descendants.- Parameters:
-
nodeFilter- the filter callbacks to perform on each node - Returns:
- this node, for chaining
-
nodeStream
Returns a Stream of this Node and all of its descendant Nodes. The stream has document order.- Returns:
- a stream of all nodes.
- Since:
- 1.17.1
- See Also:
-
nodeStream
Returns a Stream of this and descendant nodes, containing only nodes of the specified type. The stream has document order.- Returns:
- a stream of nodes filtered by type.
- Since:
- 1.17.1
- See Also:
-
outerHtml
public String outerHtml()Get the outer HTML of this node. For example, on apelement, may return<p>Para</p>.- Returns:
- outer HTML
- See Also:
-
outerHtml
protected void outerHtml(Appendable accum) -
outerHtml
-
html
public <T extends Appendable> T html(T appendable) Write this node and its children to the givenAppendable.- Parameters:
-
appendable- theAppendableto write to. - Returns:
-
the supplied
Appendable, for chaining. - Throws:
-
SerializationException- if the appendable throws an IOException.
-
sourceRange
Get the source range (start and end positions) in the original input source from which this node was parsed. Position tracking must be enabled prior to parsing the content. For an Element, this will be the positions of the start tag.- Returns:
-
the range for the start of the node, or
untrackedif its range was not tracked. - Since:
- 1.15.2
- See Also:
-
toString
public String toString()Gets this node's outer HTML.- Overrides:
-
toStringin classObject - Returns:
- outer HTML.
- See Also:
-
indent
@Deprecated protected void indent(Appendable accum, int depth, Document.OutputSettings out) throws IOException Deprecated.internal method moved into Printer; will be removed in a future version- Throws:
-
IOException
-
equals
public boolean equals(@Nullable Object o) Check if this node is the same instance of another (object identity test).For a node value equality check, see
hasSameValue(Object) - Overrides:
-
equalsin classObject - Parameters:
-
o- other object to compare to - Returns:
- true if the content of this node is the same as the other
- See Also:
-
hashCode
public int hashCode()Provides a hashCode for this Node, based on its object identity. Changes to the Node's content will not impact the result.- Overrides:
-
hashCodein classObject - Returns:
- an object identity based hashcode for this Node
-
hasSameValue
public boolean hasSameValue(@Nullable Object o) Check if this node has the same content as another node. A node is considered the same if its name, attributes and content match the other node; particularly its position in the tree does not influence its similarity.- Parameters:
-
o- other object to compare to - Returns:
- true if the content of this node is the same as the other
-
clone
Create a stand-alone, deep copy of this node, and all of its children. The cloned node will have no siblings.- If this node is a
LeafNode, the clone will have no parent. - If this node is an
Element, the clone will have a simple owningDocumentto retain the configured output settings and parser.
The cloned node may be adopted into another Document or node structure using
Element.appendChild(.Node) - Overrides:
-
clonein classObject - Returns:
- a stand-alone cloned node, including clones of any children
- See Also:
- If this node is a
-
shallowClone
Create a stand-alone, shallow copy of this node. None of its children (if any) will be cloned, and it will have no parent or sibling nodes.- Returns:
- a single independent copy of this node
- See Also:
-
doClone
-