Java XML Element Get by Attribute findElementWithUniqueAttribute(Element root, String elementName, String attribute, String attributeValue)

Here you can find the source of findElementWithUniqueAttribute(Element root, String elementName, String attribute, String attributeValue)

Description

find Element With Unique Attribute

License

GNU General Public License

Declaration

public static Element findElementWithUniqueAttribute(Element root,
            String elementName, String attribute, String attributeValue) 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 * Version: GPL 2.0/*  www  .  j  a  v a 2s .  co m*/
 *
 * The contents of this file are subject to the GNU General Public
 * License Version 2 or later (the "GPL").
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *   MiniG.org project members
 *
 * ***** END LICENSE BLOCK ***** */

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

public class Main {
    public static Element findElementWithUniqueAttribute(Element root,
            String elementName, String attribute, String attributeValue) {
        NodeList list = root.getElementsByTagName(elementName);
        for (int i = 0; i < list.getLength(); i++) {
            Element tmp = (Element) list.item(i);
            if (tmp.getAttribute(attribute).equals(attributeValue)) {
                return tmp;
            }
        }
        return null;
    }
}

Related

  1. findElement(Element parent, String elementNS, String elementName, String attrName, String attrValue)
  2. findElementByAttribute(final NodeList list, final String name, final String value)
  3. findElementsByAttribute(Element node, String tagName, String attrName, List attrValues)
  4. findElementWithAttributes(Node node, String tagName, Collection attrs)
  5. findElementWithNameAttribute(Element parent, String name)
  6. findNode(Node node, String attr, String value)
  7. findNodeByAttributeValue(NodeList nodeList, String attributeName, String attributeValue)
  8. getElementByAttribute(Element root, String tagname, String attribute, String att_value)
  9. getElementByAttribute(String attr, String value, Element root)