Java XML Child Get by Name getChildElements(Element parent, String tagName)

Here you can find the source of getChildElements(Element parent, String tagName)

Description

Gets the immediately child elements list from the parent element.

License

Apache License

Parameter

Parameter Description
parent the parent element in the element tree
tagName the specified tag name

Return

the NOT NULL immediately child elements list

Declaration

public static List<Element> getChildElements(Element parent, String tagName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

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

public class Main {
    /**/*  w ww  .  ja  va 2  s . c  o  m*/
     * Gets the immediately child elements list from the parent element.
     *
     * @param parent
     *          the parent element in the element tree
     * @param tagName
     *          the specified tag name
     * @return the NOT NULL immediately child elements list
     */
    public static List<Element> getChildElements(Element parent, String tagName) {
        NodeList nodes = parent.getElementsByTagName(tagName);
        List<Element> elements = new ArrayList<>();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node instanceof Element && node.getParentNode() == parent) {
                elements.add((Element) node);
            }
        }

        return elements;
    }
}

Related

  1. getChildElements(Element parent, String name)
  2. getChildElements(Element parent, String nsUri, String localPart)
  3. getChildElements(Element parent, String nsUri, String localPart)
  4. getChildElements(Element parent, String tagName)
  5. getChildElements(Element parent, String tagName)
  6. getChildElements(Element parent, String tagName)
  7. getChildElementsByName(Element element, String localName)
  8. getChildElementsByName(Element parent, String elemName)
  9. getChildElementsByName(Element parent, String name)