Java XML Child Get by Name getChildElementsByName(final Element parent, final String name)

Here you can find the source of getChildElementsByName(final Element parent, final String name)

Description

Returns all elements in the parent that match the specified name.

License

Open Source License

Parameter

Parameter Description
parent the parent element
name the expected name of the childs

Return

the elements, the list might be empty if there is no match

Declaration

public static List<Element> getChildElementsByName(final Element parent, final String name) 

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;

public class Main {
    /**//  ww  w  . j  a  v a 2  s.  co  m
     * Returns all elements in the parent that match the specified name.
     *
     * @param parent
     *         the parent element
     * @param name
     *         the expected name of the childs
     *
     * @return the elements, the list might be empty if there is no match
     */
    public static List<Element> getChildElementsByName(final Element parent, final String name) {
        List<Element> elements = new ArrayList<Element>();
        if (parent != null) {
            Node child = parent.getFirstChild();
            while (child != null) {
                if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(name)) {
                    elements.add((Element) child);
                }
                child = child.getNextSibling();
            }
        }
        return elements;
    }
}

Related

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