Java XML Child Get getChildElements(Element parent)

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

Description

Gets all child Element s of the given parent Element .

License

Open Source License

Parameter

Parameter Description
parent The parent element.

Return

A containing all child of the given parent .

Declaration

public static List<Element> getChildElements(Element parent) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * 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://from w ww .  j a  v a 2s. co  m
 *     BSI Business Systems Integration AG - 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 {
    /**
     * Gets all child {@link Element}s of the given parent {@link Element}.
     *
     * @param parent
     *          The parent element.
     * @return A {@link List} containing all child {@link Element} of the given parent {@link Element}.
     */
    public static List<Element> getChildElements(Element parent) {
        return getChildElements(parent, null);
    }

    /**
     * Gets all child {@link Element}s of the given parent {@link Element} having the given tag name.
     *
     * @param parent
     *          The parent {@link Element}.
     * @param tagName
     *          The tag name of the child {@link Element}s to return. May be null. In this case all child {@link Element}s
     *          are returned.
     * @return A {@link List} containing all child {@link Element} of given parent {@link Element} having the given tag
     *         name.
     */
    public static List<Element> getChildElements(Element parent, String tagName) {
        NodeList children = null;
        if (tagName == null) {
            children = parent.getChildNodes();
        } else {
            children = parent.getElementsByTagName(tagName);
        }
        List<Element> result = new ArrayList<Element>(children.getLength());
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                result.add((Element) n);
            }
        }
        return result;
    }
}

Related

  1. getChildElements(Element element)
  2. getChildElements(Element element)
  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)