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

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

Description

get First Element By Tag Name

License

Open Source License

Declaration

public static Element getFirstElementByTagName(Element parent, String tagName) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    public static Element getFirstElementByTagName(Element parent, String tagName) {
        List<Element> foundElements = getNamedChildElements(parent, tagName);
        if (foundElements.size() > 0) {
            return foundElements.get(0);
        } else {/*  ww w . j a  v a  2 s.com*/
            return null;
        }
    }

    public static List<Element> getNamedChildElements(Element parent, String name) {
        List<Element> elements = new ArrayList<Element>();
        if (parent != null) {
            Node child = parent.getFirstChild();
            while (child != null) {
                if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                    elements.add((Element) child);
                }
                child = child.getNextSibling();
            }
        }
        return elements;
    }
}

Related

  1. getElementValue(Element parent, String tagName)
  2. getFirstElement(Element parent, String name)
  3. getFirstElementAtAnyDepthByNodeName(Element parent, String name)
  4. getFirstElementByName(Element parent, String name)
  5. getFirstElementByName(String name, Element parent)
  6. getGrandSonElementByTagName(Element element, String parentName, String eleName)
  7. getGrandSonElementsByTagName(Element ele, String parentName, String eleName)
  8. getGrandSonElementValueByTagName(Element element, String parentName, String eleName)
  9. getGrandSonListValueByTagName(Element element, String parentName, String eleName)