Java XML Attribute Exist hasAttribute(Element ele, String attrName)

Here you can find the source of hasAttribute(Element ele, String attrName)

Description

judge whether element has an attribute named attrName

License

Open Source License

Parameter

Parameter Description
ele a parameter
attrName a parameter

Return

true if element has attribute called attrName ignoring case in the comparison.

Declaration

public static boolean hasAttribute(Element ele, String attrName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  ww. jav a  2 s.c  o m
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * judge whether element has an attribute named attrName
     * 
     * @param ele
     * @param attrName
     * @return true if element has attribute called attrName ignoring
     * case  in the comparison.
     */
    public static boolean hasAttribute(Element ele, String attrName) {
        NamedNodeMap map = ele.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node attr = map.item(i);
            if (attr.getNodeName().equalsIgnoreCase(attrName)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. hasAttribute(Element e, String s)
  2. hasAttribute(Element element, String attribute)
  3. hasAttribute(Element element, String namespace, String name)
  4. hasAttribute(Element element, String value)
  5. hasAttribute(final Node node, final String name)