Java XML Element Get getElementList(Element dataRoot, String name)

Here you can find the source of getElementList(Element dataRoot, String name)

Description

get Element List

License

BSD License

Parameter

Parameter Description
rootElement the starting node
subElementName the name of the subelement to find

Return

the list of all DOM Element with the provided name direct child of the starting node

Declaration

public static List<Element> getElementList(Element dataRoot, String name) 

Method Source Code

//package com.java2s;
/**/*from   w  ww  .  j  a va 2  s.  co m*/
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */

import java.util.ArrayList;

import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * 
     * @param rootElement
     *            the starting node
     * @param subElementName
     *            the name of the subelement to find
     * @return the list of all DOM Element with the provided name direct child
     *         of the starting node
     */
    public static List<Element> getElementList(Element dataRoot, String name) {
        NodeList list = dataRoot.getElementsByTagName(name);
        List<Element> listElements = new ArrayList<Element>();
        for (int i = 0; i < list.getLength(); i++) {
            Element item = (Element) list.item(i);
            if (item.getParentNode().equals(dataRoot)) {
                listElements.add(item);
            }
        }
        return listElements;
    }
}

Related

  1. getElementIndex(Element ele)
  2. getElementIndex(Element element)
  3. getElementLanguage(Element elemNode, String defaultLangCode)
  4. getElementLanguage(Element elemNode, String defaultLangCode)
  5. getElementList(Document doc, String expression)
  6. getElementList(Element dataRoot, String name)
  7. getElementList(Element element)
  8. getElementList(Element element, String name)
  9. getElementList(final Element element)