public abstract class DomUtils extends Object
Constructor and Description |
---|
DomUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
addLiteral(Element element,
String literalText)
Add literal text to the supplied element.
|
static void |
appendList(Node node,
List nodes)
Append the nodes from the supplied list to the supplied node.
|
static void |
copyChildNodes(Node source,
Node target)
Copy child node references from source to target.
|
static List |
copyNodeList(NodeList nodeList)
Copy the nodes of a NodeList into the supplied list.
|
static int |
countElementsBefore(Node node,
String tagName)
Count the DOM element nodes before the supplied node, having the specified
tag name, not including the node itself.
|
static int |
countNodesBefore(Node node)
Count the DOM nodes before the supplied node, not including the node itself.
|
static int |
countNodesBefore(Node node,
short nodeType)
Count the DOM nodes of the supplied type (nodeType) before the supplied
node, not including the node itself.
|
static int |
countNodesBetween(Node node1,
Node node2)
Count the DOM nodes between the supplied sibling nodes, not including
the nodes themselves.
|
static int |
countNodesBetween(Node node1,
Node node2,
short nodeType)
Count the DOM nodes of the supplied type (nodeType) between the supplied
sibling nodes, not including the nodes themselves.
|
static String |
getAllText(Element parent,
boolean removeEntities)
Get the combined text from all the text, comment and cdata DOM nodes
contained within the supplied parent element.
|
static String |
getAttributeValue(Element element,
String attributeName)
Get attribute value, returning
null if unset. |
static String |
getAttributeValue(Element element,
String attributeName,
String namespaceURI)
Get attribute value, returning
null if unset. |
static boolean |
getBooleanAttrib(Element element,
String attribName)
Get a boolean attribute from the supplied element.
|
static boolean |
getBooleanAttrib(Element element,
String attribName,
String namespaceURI)
Get a boolean attribute from the supplied element.
|
static int |
getDepth(Element element) |
static Element |
getElement(Element parent,
String localname,
int position)
Get the child element having the supplied localname, position
and namespace.
|
static Element |
getElement(Element parent,
String localname,
int position,
String namespaceURI)
Get the child element having the supplied localname, position
and namespace.
|
static List |
getElements(Element parent,
String localname,
String namespaceURI)
Get the child elements having the supplied localname and namespace.
|
static List |
getElements(NodeList nodeList,
String localname,
String namespaceURI)
Get the child elements having the supplied localname and namespace.
|
static String |
getName(Element element)
Get the name from the supplied element.
|
static Element |
getParentElement(Element child,
String parentLocalName)
Get the parent element of the supplied element having the
specified tag name.
|
static Element |
getParentElement(Element child,
String parentLocalName,
String namespaceURI)
Get the parent element of the supplied element having the
specified tag name.
|
static Node |
getPreviousSibling(Node node,
short nodeType) |
static String |
getTextBefore(Node node)
Get all the text DOM sibling nodes before the supplied node and
concatenate them together into a single String.
|
static String |
getTextBetween(Node node1,
Node node2)
Get all the text DOM sibling nodes before the supplied node and
concatenate them together into a single String.
|
static String |
getXPath(Node node)
Construct the XPath of the supplied DOM Node.
|
static void |
insertBefore(NodeList newNodes,
Node refNode)
Insert the supplied nodes before the supplied reference node (refNode).
|
static void |
insertBefore(Node newNode,
Node refNode)
Insert the supplied node before the supplied reference node (refNode).
|
static void |
removeChildren(Node node)
Remove all child nodes from the supplied node.
|
static void |
removeElement(Element element,
boolean keepChildren)
Remove the supplied element from its containing document.
|
static Element |
renameElement(Element element,
String replacementElement,
boolean keepChildContent,
boolean keepAttributes)
Rename element.
|
static Element |
renameElementNS(Element element,
String replacementElement,
String namespace,
boolean keepChildContent,
boolean keepAttributes)
Rename element.
|
static void |
replaceNode(NodeList newNodes,
Node oldNode)
Replace one node with a list of nodes.
|
static void |
replaceNode(NodeList newNodes,
Node oldNode,
boolean clone)
Replace one node with a list of nodes.
|
static void |
replaceNode(Node newNode,
Node oldNode)
Replace one node with another node.
|
public static void copyChildNodes(Node source, Node target)
source
- Source Node.target
- Target Node.public static void replaceNode(Node newNode, Node oldNode)
newNode
- New node - added in same location as oldNode.oldNode
- Old node - removed.public static void replaceNode(NodeList newNodes, Node oldNode)
newNodes
- New nodes - added in same location as oldNode.oldNode
- Old node - removed.public static void replaceNode(NodeList newNodes, Node oldNode, boolean clone)
newNodes
- New nodes - added in same location as oldNode.oldNode
- Old node - removed.clone
- Clone Nodelist Nodes.public static void insertBefore(Node newNode, Node refNode)
newNode
- Node to be inserted.refNode
- Reference node before which the supplied nodes should
be inserted.public static void insertBefore(NodeList newNodes, Node refNode)
newNodes
- Nodes to be inserted.refNode
- Reference node before which the supplied nodes should
be inserted.public static Element renameElement(Element element, String replacementElement, boolean keepChildContent, boolean keepAttributes)
element
- The element to be renamed.replacementElement
- The tag name of the replacement element.keepChildContent
- true
if the target element's child content
is to be copied to the replacement element, false if not. Default true
.keepAttributes
- true
if the target element's attributes
are to be copied to the replacement element, false if not. Default true
.public static Element renameElementNS(Element element, String replacementElement, String namespace, boolean keepChildContent, boolean keepAttributes)
element
- The element to be renamed.replacementElement
- The tag name of the replacement element. Can be a prefix qualified
name if the namespace is not the null namepsace (XMLConstants.NULL_NS_URI
).namespace
- The element namespace.keepChildContent
- true
if the target element's child content
is to be copied to the replacement element, false if not. Default true
.keepAttributes
- true
if the target element's attributes
are to be copied to the replacement element, false if not. Default true
.public static void removeElement(Element element, boolean keepChildren)
Node.ELEMENT_NODE
nodes.element
- Element to be removed.keepChildren
- Keep child content.public static void removeChildren(Node node)
node
- to be "cleared".public static List copyNodeList(NodeList nodeList)
nodeList
- Nodelist to copy.public static void appendList(Node node, List nodes)
node
- Node to be appended to.nodes
- List of nodes to append.public static boolean getBooleanAttrib(Element element, String attribName)
element
- The element.attribName
- The attribute name.public static boolean getBooleanAttrib(Element element, String attribName, String namespaceURI)
element
- The element.namespaceURI
- Namespace URI of the required attribute.attribName
- The attribute name.public static Element getParentElement(Element child, String parentLocalName)
child
- Child element.parentLocalName
- Parent element local name.public static Element getParentElement(Element child, String parentLocalName, String namespaceURI)
child
- Child element.parentLocalName
- Parent element local name.namespaceURI
- Namespace URI of the required parent element,
or null if a non-namespaced get is to be performed.public static String getName(Element element)
localName
of the element
if set (namespaced element), otherwise the
element's tagName
is returned.element
- The element.public static String getAttributeValue(Element element, String attributeName)
null
if unset.
Some DOM implementations return an empty string for an unset
attribute.element
- The DOM element.attributeName
- The attribute to get.null
if unset.public static String getAttributeValue(Element element, String attributeName, String namespaceURI)
null
if unset.
Some DOM implementations return an empty string for an unset
attribute.element
- The DOM element.attributeName
- The attribute to get.namespaceURI
- Namespace URI of the required attribute, or null
to perform a non-namespaced get.null
if unset.public static int countNodesBefore(Node node, short nodeType)
node
- Node whose siblings are to be counted.nodeType
- The DOM Node
type of the siblings to be counted.public static int countNodesBetween(Node node1, Node node2, short nodeType)
node1
- First sibling node.node2
- Second sibling node.nodeType
- The DOM Node
type of the siblings to be counted.UnsupportedOperationException
- if the supplied Nodes
don't have the same parent node i.e. are not sibling nodes.public static int countNodesBefore(Node node)
node
- Node whose siblings are to be counted.public static int countNodesBetween(Node node1, Node node2)
node1
- First sibling node.node2
- Second sibling node.UnsupportedOperationException
- if the supplied Nodes
don't have the same parent node i.e. are not sibling nodes.public static int countElementsBefore(Node node, String tagName)
node
- Node whose element siblings are to be counted.tagName
- The tag name of the sibling elements to be counted.public static String getTextBefore(Node node)
node
- Text node.public static String getTextBetween(Node node1, Node node2)
node1
- Test node.public static String getXPath(Node node)
node
- DOM node for XPath generation.public static int getDepth(Element element)
public static String getAllText(Element parent, boolean removeEntities)
parent
- The parent DOM element.removeEntities
- Remove all HTML entity and character references from
the DOM Text child nodes and replace them with their equivalent characters. Note
this is not performed on Comment or CDATA section nodes.public static void addLiteral(Element element, String literalText)
element
- Target DOM Element.literalText
- Literal text to be added.public static Element getElement(Element parent, String localname, int position)
parent
- Parent element to be searched.localname
- Localname of the element required.position
- The position of the element relative to other sibling
elements having the same name (and namespace if specified) e.g. if
searching for the 2nd <input> element, this param needs to
have a value of 2.public static Element getElement(Element parent, String localname, int position, String namespaceURI)
parent
- Parent element to be searched.localname
- Localname of the element required.position
- The position of the element relative to other sibling
elements having the same name (and namespace if specified) e.g. if
searching for the 2nd <input> element, this param needs to
have a value of 2.namespaceURI
- Namespace URI of the required element, or null
if a namespace comparison is not to be performed.public static List getElements(Element parent, String localname, String namespaceURI)
parent
- Parent element to be searched.localname
- Localname of the element required. Supports "*" wildcards.namespaceURI
- Namespace URI of the required element, or null
if a namespace comparison is not to be performed.Element
s. An empty list if no such
child elements exist on the parent element.public static List getElements(NodeList nodeList, String localname, String namespaceURI)
nodeList
- List of DOM nodes on which to perform the search.localname
- Localname of the element required. Supports "*" wildcards.namespaceURI
- Namespace URI of the required element, or null
if a namespace comparison is not to be performed.Element
s. An empty list if no such
child elements exist on the parent element.Copyright © 2020. All rights reserved.