Java XML Node Name getAllNodes(Node node, String[] nodeNamesArray)

Here you can find the source of getAllNodes(Node node, String[] nodeNamesArray)

Description

get All Nodes

License

Open Source License

Declaration

static public ArrayList getAllNodes(Node node, String[] nodeNamesArray) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2003, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://w  ww .  j a v  a 2  s.c om
 * IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    static public ArrayList getAllNodes(Node node, String nodeName) {
        ArrayList nodeList = new ArrayList();

        String[] nodeNames = { nodeName };
        findAllNodes(node, nodeNames, nodeList);

        return nodeList;
    }

    static public ArrayList getAllNodes(Node node, String[] nodeNamesArray) {
        ArrayList nodeList = new ArrayList();
        findAllNodes(node, nodeNamesArray, nodeList);

        return nodeList;
    }

    static private void findAllNodes(Node node, String[] nodeNames,
            ArrayList results) {

        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                for (int j = 0; j < nodeNames.length; j++) {
                    if (nodes.item(i).getNodeName().equals(nodeNames[j])) {
                        results.add(nodes.item(i));
                    }
                }
                findAllNodes(nodes.item(i), nodeNames, results);
            }
        }
    }
}

Related

  1. getBeanElement(Node elem, String nodeName)
  2. getDescendent(Node node, String nodeName)
  3. getIntegerNodeValue(Node node, String nodeName)
  4. getName(Node node)