Java XML Child Get by Name getChildElements(Element parent, String elementTag)

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

Description

Get the XML children element of an XML element, but only those of a certain type

License

Open Source License

Parameter

Parameter Description
parent The parent element to get the children from
elementTag The tag of the elements to return

Return

The list of children of the parent

Declaration

public static List<Element> getChildElements(Element parent, String elementTag) 

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 ww.  j  a  v a2s  .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 element, String childrenName)
  2. getChildElements(Element element, String namespace, String localName)
  3. getChildElements(Element element, String tag)
  4. getChildElements(Element element, String tagName)
  5. getChildElements(Element p_rootElement, String p_elementName)
  6. getChildElements(Element parent, String localName)
  7. getChildElements(Element parent, String name)
  8. getChildElements(Element parent, String name)
  9. getChildElements(Element parent, String name)