Java XML Element Get getElementsByName(Element elem, String s)

Here you can find the source of getElementsByName(Element elem, String s)

Description

get all children of given type or empty list

License

Creative Commons License

Parameter

Parameter Description
element the element to search in
s type/tag of children

Return

list of children of given type or empty, never null

Declaration

public static List<Element> getElementsByName(Element elem, String s) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**/* w  w  w .ja v  a  2 s  .c  o  m*/
     * get all children of given type or empty list
     * 
     * @param element
     *           the element to search in
     * @param s
     *           type/tag of children
     * @return list of children of given type or empty, never null
     */
    public static List<Element> getElementsByName(Element elem, String s) {
        List<Element> ret = new ArrayList<Element>();
        for (Node child = elem.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child instanceof Element) {
                if (child.getNodeName().equals(s)) {
                    ret.add((Element) child);
                }
            }
        }
        return ret;
    }
}

Related

  1. getElements(Element root)
  2. getElements(Element root, String element)
  3. getElements(Element root, String tagName)
  4. getElements(Element topElm)
  5. getElements(final String elementName, final InputStream is, final boolean onlyValues)
  6. getElementsByTag(Element element, String tag)
  7. getElementsByTagName(Element aElement, String aTagName)
  8. getElementsByTagName(Element element, String tag)
  9. getElementsByTagName(Element element, String tagName)