Java XML Element Get by Attribute getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue)

Here you can find the source of getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue)

Description

equivalent to the XPath expression './/tagName[@attrName='attrValue']'

License

BSD License

Declaration

public static Element getElementByAttributeValue(Node start, String tagName, String attrName,
        String attrValue) 

Method Source Code


//package com.java2s;
/*/*from w w  w .  j  a va2  s .  co m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import org.w3c.dom.*;

public class Main {
    /**
     * equivalent to the XPath expression './/tagName[@attrName='attrValue']'
     */
    public static Element getElementByAttributeValue(Node start, String tagName, String attrName,
            String attrValue) {
        NodeList nl = ((Element) start).getElementsByTagName(tagName);
        int l = nl.getLength();

        if (l == 0) {
            return null;
        }

        Element e = null;
        String compareValue = null;

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);

            if (e.getNodeType() == Node.ELEMENT_NODE) {
                compareValue = e.getAttribute(attrName);

                if (compareValue.equals(attrValue)) {
                    return e;
                }
            }
        }

        return null;
    }
}

Related

  1. findNode(Node node, String attr, String value)
  2. findNodeByAttributeValue(NodeList nodeList, String attributeName, String attributeValue)
  3. getElementByAttribute(Element root, String tagname, String attribute, String att_value)
  4. getElementByAttribute(String attr, String value, Element root)
  5. getElementByAttributeValue(List elements, String attName, String attValue)
  6. getElementIntAttribute(Element e, String whichAttribute)
  7. getElementsWithAttribute(Element element, String attribute)
  8. getElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)
  9. getElementsWithAttributeEquals( Element root, String attribute, String value)