Java XML Child Get getChildElements(Element ele, String childEleName)

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

Description

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

License

Open Source License

Parameter

Parameter Description
ele the DOM element to analyze
childEleName the child element getName to look for

Return

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

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source 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 {
    /**// w w w .  ja  v a 2s  .c  o m
     * Retrieve all child elements of the given DOM element that match
     * the given element getName. 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 getName 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<Node> getChildElements(Element ele,
            String childEleName) {
        NodeList nl = ele.getChildNodes();
        List<Node> childEles = new ArrayList<Node>();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element
                    && nodeNameEquals(node, childEleName))
                childEles.add(node);
        }
        return childEles;
    }

    /**
     * Namespace-aware equals comparison. Returns <code>true</code> if either
     * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>,
     * otherwise returns <code>false</code>.
     */
    public static boolean nodeNameEquals(Node node, String desiredName) {
        assert node != null : "Node must not be null";
        assert desiredName != null : "Desired getName must not be null";
        return desiredName.equals(node.getNodeName())
                || desiredName.equals(node.getLocalName());
    }
}

Related

  1. getChildElements(Element e, String tag)
  2. getChildElements(Element el)
  3. getChildElements(Element ele)
  4. getChildElements(Element ele)
  5. getChildElements(Element ele)
  6. getChildElements(Element elem, String name)
  7. getChildElements(Element element)
  8. getChildElements(Element element)
  9. getChildElements(Element element)