Java XML Element Get getElementByTagName(Element element, String tag)

Here you can find the source of getElementByTagName(Element element, String tag)

Description

get single element by tag name

License

Apache License

Parameter

Parameter Description
element a parameter
tag a parameter

Declaration

public static Element getElementByTagName(Element element, String tag) 

Method Source Code

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

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

public class Main {
    /**/* w  w  w. ja  v  a 2s  .c om*/
     * get single element by tag name
     * @param element
     * @param tag
     * @return
     */
    public static Element getElementByTagName(Element element, String tag) {
        NodeList list = element.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node instanceof Element && ((Element) node).getTagName().equals(tag)) {
                return (Element) node;
            }
        }
        return null;
    }
}

Related

  1. getElementByTagName(Element el, String tag)
  2. getElementByTagName(Element elem, String name, String emptyValue)
  3. getElementByTagName(Element element, String name)
  4. getElementByTagName(Element element, String name, int index)
  5. getElementByTagName(Element element, String tag)
  6. getElementByTagName(Element n, String elementName)
  7. getElementContent(Element e)
  8. getElementContent(Element elem)
  9. getElementContent(Element elem)