Java XML First Child Element getFirstLevelChildElementsByTagName(Element parent, String elementName)

Here you can find the source of getFirstLevelChildElementsByTagName(Element parent, String elementName)

Description

get First Level Child Elements By Tag Name

License

Open Source License

Declaration

public static final List<Element> getFirstLevelChildElementsByTagName(Element parent, String elementName) 

Method Source Code

//package com.java2s;

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 final List<Element> getFirstLevelChildElementsByTagName(Element parent, String elementName) {
        NodeList nodeList = parent.getChildNodes();

        ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength());

        Node childNode = parent.getFirstChild();
        while (childNode != null) {
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                String localName = ((Element) childNode).getLocalName();
                if (localName.equals(elementName)) {
                    childList.add((Element) childNode);
                }/*  ww  w.j a  va2 s  . com*/
            }
            childNode = childNode.getNextSibling();
        }

        return childList;
    }
}

Related

  1. getFirstLevelChildElementByTagName(Element parent, String elementName)
  2. getFirstLevelChildElementByTagName(Element parent, String elementName)
  3. getFirstLevelChildElements(Element parent)
  4. getFirstLevelChildElements(Element parent)
  5. getFirstLevelChildElementsByTagName(Element parent, String elementName)
  6. getFirstMatchedValueByChildTagName(Node parent, String name)
  7. getFirstMatchingDeepChildByTagName(final Element e, final String tagName)
  8. getFirstNamedChild(Node n, String name)
  9. getFirstNamedChild(Node node, String name)