Java XML First Child Element getFirstLevelChildElements(Element parent)

Here you can find the source of getFirstLevelChildElements(Element parent)

Description

Get all the first level child elements.

License

Open Source License

Parameter

Parameter Description
parent Parent Element

Return

List of all the child Elements.

Declaration

public static final List<Element> getFirstLevelChildElements(Element parent) 

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 {
    /**/*from w w  w.  j  av a 2s .c o  m*/
     * Get all the first level child elements.
     * 
     * @param parent
     *            Parent Element
     * 
     * @return List of all the child Elements.
     */
    public static final List<Element> getFirstLevelChildElements(Element parent) {
        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) {
                childList.add((Element) childNode);
            }
            childNode = childNode.getNextSibling();
        }

        return childList;
    }
}

Related

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