Java XML Child Node Get getChildNodesByName(Node element, String name, boolean caseSensitive)

Here you can find the source of getChildNodesByName(Node element, String name, boolean caseSensitive)

Description

Helper Method.

License

Open Source License

Parameter

Parameter Description
element Element
name String
caseSensitive boolean

Return

ArrayList

Declaration

public static List<Node> getChildNodesByName(Node element, String name, boolean caseSensitive) 

Method Source Code

//package com.java2s;
/**//from w  ww . j a va  2s  .c  o  m
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import java.util.ArrayList;
import java.util.List;

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

public class Main {
    /**
     * Helper Method. Searches through the child nodes of an element and returns an array of the matching nodes.
     * @param element Element
     * @param name String
     * @param caseSensitive boolean
     * @return ArrayList
     */
    public static List<Node> getChildNodesByName(Node element, String name, boolean caseSensitive) {
        ArrayList<Node> nodes = new ArrayList<Node>();
        NodeList list = element.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (caseSensitive) {
                if (node.getNodeName().equals(name))
                    nodes.add(node);
            } else {
                if (node.getNodeName().equalsIgnoreCase(name))
                    nodes.add(node);
            }
        }
        return nodes;
    }
}

Related

  1. getChildNodes(Node parentNode)
  2. getChildNodes(Node parentNode, String childName)
  3. getChildNodes(Node parentNode, String childNodeName)
  4. getChildNodes(Node rootNode, String childName)
  5. getChildNodesByName(Element parent, String name)
  6. getChildNodesByName(Node element, String name, boolean caseSensitive)
  7. getChildNodesByName(Node element, String name, boolean caseSensitive)
  8. getChildNodesByName(Node node, String name)
  9. getChildNodesByTagName(final Element element, final String tagName)