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

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

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

Return

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

Declaration

public static List<Element> getChildElementsByTagName(Element ele, String childEleName) 

Method Source Code


//package com.java2s;
/*//from  w ww  . j  ava 2 s . c o  m
 * Copyright 2002-2008 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.Arrays;
import java.util.Collection;
import java.util.List;
import org.springframework.util.Assert;

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 any of the given element names. 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 childEleNames the child element names 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<Element> getChildElementsByTagName(Element ele, String[] childEleNames) {
        Assert.notNull(ele, "Element must not be null");
        Assert.notNull(childEleNames, "Element names collection must not be null");
        List<String> childEleNameList = Arrays.asList(childEleNames);
        NodeList nl = ele.getChildNodes();
        List<Element> childEles = new ArrayList<Element>();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
                childEles.add((Element) node);
            }
        }
        return childEles;
    }

    /**
     * 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<Element> getChildElementsByTagName(Element ele, String childEleName) {
        return getChildElementsByTagName(ele, new String[] { childEleName });
    }

    /** Matches the given node's name and local name against the given desired name. */
    private static boolean nodeNameMatch(Node node, String desiredName) {
        return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()));
    }

    /** Matches the given node's name and local name against the given desired names. */
    private static boolean nodeNameMatch(Node node, Collection desiredNames) {
        return (desiredNames.contains(node.getNodeName()) || desiredNames.contains(node.getLocalName()));
    }
}

Related

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