Java XML Attribute from Element getElement(Element root, String tagName, String attrName, String attrValue)

Here you can find the source of getElement(Element root, String tagName, String attrName, String attrValue)

Description

get Element

License

Open Source License

Declaration

public static Element getElement(Element root, String tagName, String attrName, String attrValue) 

Method Source Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    public static Element getElement(Element root, String tagName, String attrName, String attrValue) {
        if (root.getTagName().equals(tagName)) {
            if (attrName == null) {
                return (root);
            }/* www . j  a  v a  2s.c om*/
            if (root.getAttribute(attrName).equals(attrValue)) {
                return (root);
            }
        }

        NodeList list = root.getElementsByTagName(tagName);

        for (int i = 0; i < list.getLength(); i++) {
            Element el = (Element) list.item(i);
            if (attrName == null) {
                return (el);
            }
            if (el.getAttribute(attrName).equals(attrValue)) {
                return (el);
            }
        }

        return (null);
    }
}

Related

  1. getElementArrayInt(Element root, String name, String attrib)
  2. getElementArrayString(Element root, String name, String attrib)
  3. getElementAttr(Element element, String attr)
  4. getElementAttribute(Element element, String name)