Java XML Child Get by Name getChildElementsByTagName(Element ele, String childEleName, boolean localName)

Here you can find the source of getChildElementsByTagName(Element ele, String childEleName, boolean localName)

Description

Retrieve all child elements of the given DOM element that match the given element name.

License

Apache License

Parameter

Parameter Description
ele the DOM element to analyze
childEleName the child element name to look for
localName whether to compare against the local node name

Return

a List of child org.w3c.dom.Element instances

Declaration

public static List getChildElementsByTagName(Element ele, String childEleName, boolean localName) 

Method Source Code


//package com.java2s;
/*//from  ww w  .j a v  a 2 s.c om
 * Copyright 2004-2009 the original author or authors.
 *
 * 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 java.util.List;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * Retrieve all child elements of the given DOM element that match
     * the given element name. Only look at the direct child level of the
     * given element; do not go into further depth (in contrast to the
     * DOM API's <code>getElementsByTagName</code> method).
     *
     * @param ele          the DOM element to analyze
     * @param childEleName the child element name to look for
     * @return a List of child <code>org.w3c.dom.Element</code> instances
     * @see org.w3c.dom.Element
     * @see org.w3c.dom.Element#getElementsByTagName
     */
    public static List getChildElementsByTagName(Element ele, String childEleName) {
        return getChildElementsByTagName(ele, childEleName, false);
    }

    /**
     * Retrieve all child elements of the given DOM element that match
     * the given element name. Only look at the direct child level of the
     * given element; do not go into further depth (in contrast to the
     * DOM API's <code>getElementsByTagName</code> method).
     * <p/>Can select whether or not to compare against the local name of
     * the {@link org.w3c.dom.Element}
     *
     * @param ele          the DOM element to analyze
     * @param childEleName the child element name to look for
     * @param localName    whether to compare against the local node name
     * @return a List of child <code>org.w3c.dom.Element</code> instances
     * @see org.w3c.dom.Element
     * @see org.w3c.dom.Element#getElementsByTagName
     */
    public static List getChildElementsByTagName(Element ele, String childEleName, boolean localName) {
        NodeList nl = ele.getChildNodes();
        List childEles = new ArrayList();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            String name = (localName) ? node.getLocalName() : node.getNodeName();
            if (node instanceof Element && childEleName.equals(name)) {
                childEles.add(node);
            }
        }
        return childEles;
    }
}

Related

  1. getChildElementsByName(Element parent, String tagName)
  2. getChildElementsByName(Element root, String tagName)
  3. getChildElementsByName(final Element parent, final String name)
  4. getChildElementsByTagName(Element ele, String childEleName)
  5. getChildElementsByTagName(Element ele, String childEleName)
  6. getChildElementsByTagName(Element elem, String name)
  7. getChildElementsByTagName(Element element, String name)
  8. getChildElementsByTagName(Element element, String tagName)
  9. getChildElementsByTagName(Element parentElement, String childTag)