Java XML Node Type getNodeByType(Node root, int type)

Here you can find the source of getNodeByType(Node root, int type)

Description

get Node By Type

License

Open Source License

Declaration

public static Node getNodeByType(Node root, int type) 

Method Source Code

//package com.java2s;
/*/*w ww  .java  2 s.c om*/
The MIT License (MIT)
    
Copyright (c) 2015 NuBean LLC
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import java.util.*;

import org.w3c.dom.*;

public class Main {
    public static Node getNodeByType(Node root, int 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)
                return node;

            NodeList nodeList = node.getChildNodes();
            int count = (nodeList != null ? nodeList.getLength() : 0);
            if (count == 0) {
                nodeStack.pop();
                continue;
            } else {
                Stack<Integer> indexStack = stackMap.get(node);
                if (indexStack == null) {
                    indexStack = new Stack<Integer>();
                    stackMap.put(node, indexStack);
                    nodeStack.push(nodeList.item(0));
                    indexStack.push(new Integer(1));
                    continue;
                } else {
                    int top = ((Integer) indexStack.pop()).intValue();
                    if (top < count) {
                        nodeStack.push(nodeList.item(top));
                        indexStack.push(new Integer(top + 1));
                        continue;
                    } else {
                        Object key = nodeStack.pop();
                        stackMap.remove(key);
                        continue;
                    }
                }
            }
        }

        return null;
    }
}

Related

  1. containsNodeType(org.w3c.dom.Node node, short nodeType)
  2. countNodesBefore(Node node, short nodeType)
  3. countNodesBetween(Node node1, Node node2, short nodeType)
  4. getMethodArgValue(final Node typeNode, final String value)
  5. getNodesOfType( NamedNodeMap list, short type)
  6. getNodeType(Node node)
  7. getNodeType(Node node)
  8. getNodeType(Node node)