Class DOMUtil
- java.lang.Object
-
- org.apache.tools.ant.taskdefs.optional.junit.DOMUtil
-
public final class DOMUtil extends java.lang.ObjectSome utilities that might be useful when manipulating DOM trees.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDOMUtil.NodeFilterFilter interface to be applied when iterating over a DOM tree.static classDOMUtil.NodeListImplcustom implementation of a nodelist
-
Method Summary
Modifier and Type Method Description static org.w3c.dom.ElementgetChildByTagName(org.w3c.dom.Node parent, java.lang.String tagname)Iterate over the children of a given node and return the first node that has a specific name.static java.lang.StringgetNodeAttribute(org.w3c.dom.Node node, java.lang.String name)return the attribute value of an element.static org.w3c.dom.NodeimportNode(org.w3c.dom.Node parent, org.w3c.dom.Node child)Simple tree walker that will clone recursively a node.static org.w3c.dom.NodeListlistChildNodes(org.w3c.dom.Node parent, DOMUtil.NodeFilter filter, boolean recurse)list a set of node that match a specific filter.
-
-
-
Method Detail
-
listChildNodes
public static org.w3c.dom.NodeList listChildNodes(org.w3c.dom.Node parent, DOMUtil.NodeFilter filter, boolean recurse)list a set of node that match a specific filter. The list can be made recursively or not.- Parameters:
parent- the parent node to search fromfilter- the filter that children should match.recurse-trueif you want the list to be made recursively otherwisefalse.- Returns:
- the node list that matches the filter.
-
getNodeAttribute
public static java.lang.String getNodeAttribute(org.w3c.dom.Node node, java.lang.String name)return the attribute value of an element.- Parameters:
node- the node to get the attribute from.name- the name of the attribute we are looking for the value.- Returns:
- the value of the requested attribute or
nullif the attribute was not found or ifnodeis not anElement.
-
getChildByTagName
public static org.w3c.dom.Element getChildByTagName(org.w3c.dom.Node parent, java.lang.String tagname)Iterate over the children of a given node and return the first node that has a specific name.- Parameters:
parent- the node to search child from. Can benull.tagname- the child name we are looking for. Cannot benull.- Returns:
- the first child that matches the given name or
nullif the parent isnullor if a child does not match the given name.
-
importNode
public static org.w3c.dom.Node importNode(org.w3c.dom.Node parent, org.w3c.dom.Node child)Simple tree walker that will clone recursively a node. This is to avoid using parser-specific API such as Sun'schangeNodeOwnerwhen we are dealing with DOM L1 implementations sincecloneNode(boolean)will not change the owner document.changeNodeOwneris much faster and avoid the costly cloning process.importNodeis in the DOM L2 interface.- Parameters:
parent- the node parent to which we should do the import to.child- the node to clone recursively. Its clone will be appended toparent.- Returns:
- the cloned node that is appended to
parent
-
-