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

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

Description

Get all child elements of a certain tag name.

License

Apache License

Parameter

Parameter Description
parent The element from which the children elements must be retrieved.
tagName The tag name of the children elements that must be retrieved

Return

A list of children elements. If no elements of the specified tag name are found, an empty list is returned.

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Apache 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   w  w  w  .  jav a 2 s .  c om*/
     * Get all child elements of a certain tag name.
     * 
     * @param parent
     *            The element from which the children elements must be
     *            retrieved.
     * @param tagName
     *            The tag name of the children elements that must be retrieved
     * @return A list of children elements. If no elements of the specified tag
     *         name are found, an empty list is returned.
     */
    public static List<Element> getChildElementsByName(Element parent, String tagName) {
        List<Element> elementList = new LinkedList<Element>();
        NodeList children = parent.getChildNodes();
        for (int c = 0; c < children.getLength(); c++) {
            if (children.item(c).getNodeType() == Node.ELEMENT_NODE
                    && children.item(c).getNodeName().equals(tagName)) {
                elementList.add((Element) children.item(c));
            }
        }
        return elementList;
    }
}

Related

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