Java XML Child Get getChildElements(Element parent)

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

Description

Get only the XML element children of an XML element.

License

Open Source License

Parameter

Parameter Description
parent The parent element to get children from

Return

The list of children Element of the parent

Declaration

public static List<Element> getChildElements(Element parent) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 ?cole Polytechnique de Montr?al
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*w w  w .j  av  a  2  s  .c o m*/
 *   Genevi?ve Bastien - Initial API and implementation
 *******************************************************************************/

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 {
    /**
     * Get only the XML element children of an XML element.
     *
     * @param parent
     *            The parent element to get children from
     * @return The list of children Element of the parent
     */
    public static List<Element> getChildElements(Element parent) {
        NodeList childNodes = parent.getChildNodes();
        List<Element> childElements = new ArrayList<>();
        for (int index = 0; index < childNodes.getLength(); index++) {
            if (childNodes.item(index).getNodeType() == Node.ELEMENT_NODE) {
                childElements.add((Element) childNodes.item(index));
            }
        }
        return childElements;
    }

    /**
     * Get the XML children element of an XML element, but only those of a
     * certain type
     *
     * @param parent
     *            The parent element to get the children from
     * @param elementTag
     *            The tag of the elements to return
     * @return The list of children {@link Element} of the parent
     */
    public static List<Element> getChildElements(Element parent, String elementTag) {
        /* get the state providers and find the corresponding one */
        NodeList nodes = parent.getElementsByTagName(elementTag);
        List<Element> childElements = new ArrayList<>();

        for (int i = 0; i < nodes.getLength(); i++) {
            Element node = (Element) nodes.item(i);
            if (node.getParentNode().equals(parent)) {
                childElements.add(node);
            }
        }
        return childElements;
    }
}

Related

  1. getChildElements(Element parent)
  2. getChildElements(Element parent)
  3. getChildElements(Element parent)
  4. getChildElements(Element parent)
  5. getChildElements(Element parent)
  6. getChildElements(Element parent)
  7. getChildElements(Element parent)
  8. getChildElements(Element parent)
  9. getChildElements(Element parent)