Java XML Child Get by Name getChildElements(Element element, String childrenName)

Here you can find the source of getChildElements(Element element, String childrenName)

Description

Given an XML element, this method returns nested elements which are direct children of the given element - but only those that have the element-name "childrenName" (the second parameter).

License

Open Source License

Parameter

Parameter Description
element a parameter
childrenName a parameter

Declaration

public static List<Element> getChildElements(Element element, String childrenName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.LinkedList;
import java.util.List;

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

public class Main {
    /**/*from   www .  j av a2s.c om*/
     * Given an XML element, this method returns nested elements which are direct
     * children of the given element - but only those that have the element-name "childrenName" (the second parameter).
     * @param element
     * @param childrenName
     * @return
     */
    public static List<Element> getChildElements(Element element, String childrenName) {
        List<Element> ret = new LinkedList<Element>();
        NodeList nodeList = element.getChildNodes();
        for (int index = 0; index < nodeList.getLength(); ++index) {
            Node node = nodeList.item(index);
            if (node instanceof Element) {
                Element elementOfNode = (Element) node;
                if (elementOfNode.getTagName().equals(childrenName)) {
                    ret.add(elementOfNode);
                }

            }
        }
        return ret;
    }
}

Related

  1. getChildElement(Node parent, String name)
  2. getChildElement(Node start, String name)
  3. getChildElement(NodeList childs, Node parent)
  4. getChildElement(String name, Element el)
  5. getChildElement(String name, Element elem)
  6. getChildElements(Element element, String namespace, String localName)
  7. getChildElements(Element element, String tag)
  8. getChildElements(Element element, String tagName)
  9. getChildElements(Element p_rootElement, String p_elementName)