Java Utililty Methods XML Node Type

List of utility methods to do XML Node Type

Description

The list of methods to do XML Node Type are organized into topic(s).

Method

booleancontainsNodeType(org.w3c.dom.Node node, short nodeType)
contains Node Type
if (node.getNodeType() == nodeType
        || (nodeType == org.w3c.dom.Node.ATTRIBUTE_NODE && node.getAttributes().getLength() > 0))
    return true;
else {
    NodeList childNodes = node.getChildNodes();
    for (int childIndex = 0; childIndex < childNodes.getLength(); childIndex++)
        if (containsNodeType(childNodes.item(childIndex), nodeType))
            return true;
...
intcountNodesBefore(Node node, short nodeType)
Count the DOM nodes of the supplied type (nodeType) before the supplied node, not including the node itself.
Node parent = node.getParentNode();
if (parent == null) {
    System.out.println("Cannot count nodes before [" + node + "]. [" + node + "] has no parent.");
    return 0;
NodeList siblings = parent.getChildNodes();
int count = 0;
int siblingCount = siblings.getLength();
...
intcountNodesBetween(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.
Node parent1 = node1.getParentNode();
if (parent1 == null) {
    System.out.println("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. [" + node1
            + "] has no parent.");
    return 0;
Node parent2 = node2.getParentNode();
if (parent2 == null) {
...
ObjectgetMethodArgValue(final Node typeNode, final String value)
get Method Arg Value
if (typeNode != null) {
    final String type = typeNode.getTextContent();
    if ("Long".equals(type) || "long".equals(type)) {
        return Long.parseLong(value);
    } else if ("Integer".equals(type) || "int".equals(type)) {
        return Integer.parseInt(value);
return value;
NodegetNodeByType(Node root, int type)
get Node By Type
if (root == null)
    return null;
Stack<Node> nodeStack = new Stack<Node>();
HashMap<Node, Stack<Integer>> stackMap = new HashMap<Node, Stack<Integer>>(17, 0.85f);
nodeStack.push(root);
while (!nodeStack.empty()) {
    org.w3c.dom.Node node = (org.w3c.dom.Node) nodeStack.peek();
    if (node.getNodeType() == type)
...
ListgetNodesOfType(NamedNodeMap list, short type)
get Nodes Of Type
List<T> retVal = new ArrayList<T>(list.getLength());
for (int i = 0; i < list.getLength(); ++i) {
    Node n = list.item(i);
    if (n.getNodeType() == type) {
        retVal.add((T) n);
return retVal;
...
shortgetNodeType(Node node)
get Node Type
return (node != null ? node.getNodeType() : -1);
shortgetNodeType(Node node)
get Node Type
return (node != null ? node.getNodeType() : -1);
shortgetNodeType(Node node)
get Node Type
return (node != null ? node.getNodeType() : -1);
StringgetNodeTypeStr(int nodeType)
Gets Node type String for a given node type constant
switch (nodeType)
case Node.ATTRIBUTE_NODE:
    return "ATTRIBUTE_NODE ";
case Node.CDATA_SECTION_NODE:
    return "CDATA_SECTION_NODE";
case Node.COMMENT_NODE:
    return "COMMENT_NODE";
...