Java XML NodeList getNodeList(NodeList nl, String[] names, int pos)

Here you can find the source of getNodeList(NodeList nl, String[] names, int pos)

Description

Get the list of nodes matching the given array of tag names in the given node list

License

Apache License

Parameter

Parameter Description
nl a parameter
names a parameter
pos a parameter

Declaration

private static ArrayList<Node> getNodeList(NodeList nl, String[] names, int pos) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .jav  a 2s .  c  o m*/
Copyright 2014-2015 Intecs Spa
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

import java.util.ArrayList;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the list of nodes matching the given path in the given node list
     * @param nl
     * @param path
     * @return
     */
    public static ArrayList<Node> getNodeList(NodeList nl, String path) {
        if ((nl == null) || (path == null))
            return null;
        if (nl.getLength() == 0)
            return null;
        String[] names = path.split("/");
        if (names.length == 0)
            return null;

        return getNodeList(nl, names, 0);
    }

    /**
     * Get the list of nodes matching the given array of tag names in the given node list
     * @param nl
     * @param names
     * @param pos
     * @return
     */
    private static ArrayList<Node> getNodeList(NodeList nl, String[] names, int pos) {
        if ((nl == null) || (names.length - pos == 0))
            return null;
        if (nl.getLength() == 0)
            return null;

        ArrayList<Node> result = null;

        if (names.length - pos == 1)
            result = new ArrayList<Node>();

        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeName().compareTo(names[pos]) == 0) {

                if (names.length - pos == 1) {
                    result.add(nl.item(i));
                } else {
                    return getNodeList(nl.item(i).getChildNodes(), names, pos + 1);
                }
            }
        }
        return result;
    }
}

Related

  1. getNodeIntValue(String tagName, NodeList nodes)
  2. getNodeList(Element from, String elementName)
  3. getNodeList(Node fatherNode, String nodeName)
  4. getNodeList(Node node, String tagName)
  5. getNodeList(Node pNode, String tagName)
  6. getNodeList(NodeList nodeList)
  7. getNodeList(NodeList nodeList)
  8. getNodeListAsList(NodeList nl)
  9. getNodeListByName(Node node, String name, List finalNodeList)