Java XML Child Get getChildElements(Element parentNode)

Here you can find the source of getChildElements(Element parentNode)

Description

return all the child Elements of given Element; it's not deep search, so only the first generation children list is scanned.

License

Open Source License

Parameter

Parameter Description
parentNode a parameter

Exception

Parameter Description
DOMException an exception

Return

an empty list if nothing found

Declaration

public static List<Element> getChildElements(Element parentNode) throws DOMException 

Method Source Code

//package com.java2s;

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

import org.w3c.dom.DOMException;

import org.w3c.dom.Element;

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

public class Main {
    /**/*from w w w  . j  a  v a 2s  .  c  om*/
     * return all the child Elements of given Element; it's not deep search, so only the first generation children list is scanned.
     * 
     * @param parentNode
     * @return an empty list if nothing found
     * @throws DOMException
     */
    public static List<Element> getChildElements(Element parentNode) throws DOMException {
        if (parentNode == null) {
            return null;
        }
        List<Element> resultList = new ArrayList<Element>();
        NodeList childNodes = parentNode.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node node = childNodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && node.getParentNode() == parentNode) {
                    resultList.add((Element) node);
                }
            }
        }
        return resultList;
    }
}

Related

  1. getChildElements(Element parent)
  2. getChildElements(Element parent)
  3. getChildElements(Element parentElement)
  4. getChildElements(Element parentElement)
  5. getChildElements(Element parentElement, String childElementTagName)
  6. getChildElements(Element root, String name, boolean mandatory)
  7. getChildElements(final Element elem)
  8. getChildElements(final Element element)
  9. getChildElements(final Element element, final String localName)