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

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

Description

Gets all child Element s of the given parent Element having the given tag name.

License

Open Source License

Parameter

Parameter Description
parent The parent Element .
tagName The tag name of the child Element s to return. May be null. In this case all child Element s are returned.

Return

A containing all child of given parent having the given tag name.

Declaration

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

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  .  ja  va 2 s.  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 parent, String name)
  2. getChildElements(Element parent, String name)
  3. getChildElements(Element parent, String nsUri, String localPart)
  4. getChildElements(Element parent, String nsUri, String localPart)
  5. getChildElements(Element parent, String tagName)
  6. getChildElements(Element parent, String tagName)
  7. getChildElements(Element parent, String tagName)
  8. getChildElementsByName(Element element, String localName)
  9. getChildElementsByName(Element parent, String elemName)