Java XML Has Child hasChildElementWithAttribute(Element element, String attributeName, String attributeValue)

Here you can find the source of hasChildElementWithAttribute(Element element, String attributeName, String attributeValue)

Description

Looks for a childelement with an attribute with the given name and value

License

Apache License

Parameter

Parameter Description
element the element to look in
attributeName the name of the attribute
attributeValue the value of the attribute

Return

true if the given element has a child element with an attribute where attrname.equals(attributeName) & attr.value(attributeValue), else false

Declaration

public static boolean hasChildElementWithAttribute(Element element, String attributeName,
        String attributeValue) 

Method Source Code

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

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

public class Main {
    /**//  w w w.  ja v a 2  s.  c o m
     * Looks for a childelement with an attribute with the given name and value
     * 
     * @param element the element to look in
     * @param attributeName the name of the attribute
     * @param attributeValue the value of the attribute
     * @return true if the given element has a child element with an attribute
     *         where attrname.equals(attributeName) &
     *         attr.value(attributeValue), else false
     */
    public static boolean hasChildElementWithAttribute(Element element, String attributeName,
            String attributeValue) {
        if (element == null) {
            return false;
        }
        for (int i = 0; i < element.getChildNodes().getLength(); i++) {
            Node child = element.getChildNodes().item(i);
            if ((child.getAttributes().getNamedItem(attributeName) != null)
                    && child.getAttributes().getNamedItem(attributeName).getNodeValue().equals(attributeValue)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. hasChildElements(Element element)
  2. hasChildElements(Node node)
  3. hasChildElements(Node node)
  4. hasChildElements(Node node)
  5. hasChildElements(Node node)
  6. hasChildNode(Element root, String childNodeName)
  7. hasChildNode(Node parentNode, String nodeName)
  8. hasChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive)
  9. hasChildNodes(Element element, String name)