Java XML First Child Element getFirstLevelChildElementsByTagName(Element parent, String elementName)

Here you can find the source of getFirstLevelChildElementsByTagName(Element parent, String elementName)

Description

Get all the first level child elements with a given tag name.

License

Open Source License

Parameter

Parameter Description
parent Parent Element
elementName List of all the first level child Elements with a given tag name.

Return

List of all the first level child elements with a given tag name.

Declaration

public static final List<Element> getFirstLevelChildElementsByTagName(Element parent, String elementName) 

Method Source Code

//package com.java2s;

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 .  j  a va2 s.co m
     * Get all the first level child elements with a given tag name.
     * 
     * @param parent
     *            Parent Element
     * @param elementName
     *            List of all the first level child Elements with a given tag name.
     * 
     * @return List of all the first level child elements with a given tag name.
     */
    public static final List<Element> getFirstLevelChildElementsByTagName(Element parent, String elementName) {
        NodeList nodeList = parent.getChildNodes();

        ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength());

        Node childNode = parent.getFirstChild();
        while (childNode != null) {
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                String localName = ((Element) childNode).getLocalName();
                if (localName.equals(elementName)) {
                    childList.add((Element) childNode);
                }
            }
            childNode = childNode.getNextSibling();
        }

        return childList;
    }
}

Related

  1. getFirstElemText(Element parent, String childName)
  2. getFirstLevelChildElementByTagName(Element parent, String elementName)
  3. getFirstLevelChildElementByTagName(Element parent, String elementName)
  4. getFirstLevelChildElements(Element parent)
  5. getFirstLevelChildElements(Element parent)
  6. getFirstLevelChildElementsByTagName(Element parent, String elementName)
  7. getFirstMatchedValueByChildTagName(Node parent, String name)
  8. getFirstMatchingDeepChildByTagName(final Element e, final String tagName)
  9. getFirstNamedChild(Node n, String name)