public abstract class Node extends Object implements Cloneable
Modifier | Constructor and Description |
---|---|
protected |
Node()
Default constructor.
|
protected |
Node(String baseUri) |
protected |
Node(String baseUri, Attributes attributes)
Create a new Node.
|
Modifier and Type | Method and Description |
---|---|
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 its 0-based index.
|
List<Node> |
childNodes()
Get this node's children.
|
protected Node[] |
childNodesAsArray() |
List<Node> |
childNodesCopy()
Returns a deep copy of this node's children.
|
int |
childNodeSize()
Get the number of child nodes that this node holds.
|
Object |
clone()
Create a stand-alone, deep copy of this node, and all of its children.
|
protected Node |
doClone(Node parent) |
protected void |
ensureChildNodes() |
boolean |
equals(Object o)
Check if this node is the same instance of another (object identity test).
|
boolean |
hasAttr(String attributeKey)
Test if this element has an attribute.
|
boolean |
hasSameValue(Object o)
Check if this node is has the same content as another node.
|
Appendable |
html(Appendable appendable)
Write this node and its children to the given Appendable .
|
protected void |
indent(Appendable 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(Appendable accum) |
Document |
ownerDocument()
Gets the Document associated with this Node.
|
Node |
parent()
Gets this node's parent node.
|
Node |
parentNode()
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 |
reparentChild(Node child) |
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 and all of its descendants.
|
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 |
traverse(NodeVisitor nodeVisitor)
Perform a depth-first traversal through this node and its descendants.
|
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.
|
protected Node(String baseUri, Attributes attributes)
baseUri
- base URI
attributes
- attributes (not null, but may be empty)
protected Node(String baseUri)
protected Node()
public abstract String nodeName()
public String attr(String attributeKey)
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.
String url = a.attr("abs:href");
attributeKey
- The attribute key.
attributes()
, hasAttr(String)
, absUrl(String)
public Attributes attributes()
public Node attr(String attributeKey, String attributeValue)
attributeKey
- The attribute key.
attributeValue
- The attribute value.
public boolean hasAttr(String attributeKey)
attributeKey
- The attribute key to check.
public Node removeAttr(String attributeKey)
attributeKey
- The attribute to remove.
public String baseUri()
public void setBaseUri(String baseUri)
baseUri
- base URI to set
public String absUrl(String attributeKey)
or
).
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");
attributeKey
- The attribute key
attr(java.lang.String)
, URL.URL(java.net.URL, String)
public Node childNode(int index)
index
- index of child node
IndexOutOfBoundsException
if the index is out of bounds.
public List<Node> childNodes()
public List<Node> childNodesCopy()
public final int childNodeSize()
protected Node[] childNodesAsArray()
public Node parent()
public final Node parentNode()
public Document ownerDocument()
public void remove()
public Node before(String html)
html
- HTML to add before this node
after(String)
public Node before(Node node)
node
- to add before this node
after(Node)
public Node after(String html)
html
- HTML to add after this node
before(String)
public Node after(Node node)
node
- to add after this node
before(Node)
public Node wrap(String html)
html
- HTML to wrap around this element, e.g.
. Can be arbitrarily deep.
public Node unwrap()
For example, with the input html:
element.unwrap()
on the span
element will result in the html:
"Two "
TextNode
being returned.
remove()
, wrap(String)
public void replaceWith(Node in)
in
- the node that will will replace the existing node.
protected void setParentNode(Node parentNode)
protected void removeChild(Node out)
protected void addChildren(Node... children)
protected void addChildren(int index, Node... children)
protected void ensureChildNodes()
protected void reparentChild(Node child)
public List<Node> siblingNodes()
node.parent.childNodes()
, but does not include this node (a node is not a sibling of itself).
public Node nextSibling()
public Node previousSibling()
public int siblingIndex()
Element.elementSiblingIndex()
protected void setSiblingIndex(int siblingIndex)
public Node traverse(NodeVisitor nodeVisitor)
nodeVisitor
- the visitor callbacks to perform on each node
public String outerHtml()
protected void outerHtml(Appendable accum)
public Appendable html(Appendable appendable)
Appendable
.
appendable
- the Appendable
to write to.
Appendable
, for chaining.
protected void indent(Appendable accum, int depth, Document.OutputSettings out) throws IOException
IOException
public boolean equals(Object o)
equals
in class Object
o
- other object to compare to
to compare nodes by their value
public boolean hasSameValue(Object o)
o
- other object to compare to
public Object clone()
The cloned node may be adopted into another Document or node structure using Element.appendChild(Node)
.
Copyright © 1998–2019 iText Group NV. All rights reserved.