Java XML Child Get by Name getChildElementsByTagName(Element element, String name)

Here you can find the source of getChildElementsByTagName(Element element, String name)

Description

get Child Elements By Tag Name

License

Apache License

Declaration

public static List<Element> getChildElementsByTagName(Element element, String name) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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 {
    public static List<Element> getChildElementsByTagName(Element element, String name) {
        List<Element> result = new ArrayList<Element>();

        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node instanceof Element) {
                String nodeName = node.getNodeName();
                if (nodeName != null) {
                    if (nodeName.equals(name)) {
                        result.add((Element) childNodes.item(i));
                    }/*  w  w  w .java  2  s. c o m*/
                }

            }
        }

        if (result.size() == 0) {
            result = null;
        }
        return result;
    }
}

Related

  1. getChildElementsByName(final Element parent, final String name)
  2. getChildElementsByTagName(Element ele, String childEleName)
  3. getChildElementsByTagName(Element ele, String childEleName)
  4. getChildElementsByTagName(Element ele, String childEleName, boolean localName)
  5. getChildElementsByTagName(Element elem, String name)
  6. getChildElementsByTagName(Element element, String tagName)
  7. getChildElementsByTagName(Element parentElement, String childTag)
  8. getChildElementsByTagName(Element parentElement, String name)
  9. getChildElementsByTagName(Element parentNode, String tagName)