Java XML Attribute Get getAttributeValueIgnoreCase(Element element, String attributeName)

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

Description

Gets the value of DOM attribute of element with the given name It returns the first attribute value that matches the name, regardless the case.

License

Open Source License

Parameter

Parameter Description
element the source element
attributeName the attribute name

Return

the XML attribute if any, null otherwise

Declaration

public static String getAttributeValueIgnoreCase(Element element, String attributeName) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j  a  v  a 2s  . c om
 * ? Copyright IBM Corp. 2012
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * Gets the value of DOM attribute of <code>element</code> with the given name
     * It returns the first attribute value that matches the name, regardless the case.
     * This is particularly useful when dealing with an HTML DOM, as HTML is not
     * case sensitive.
     * @param element the source element
     * @param attributeName the attribute name
     * @return the XML attribute if any, null otherwise
     */
    public static String getAttributeValueIgnoreCase(Element element, String attributeName) {
        Attr a = getAttributeIgnoreCase(element, attributeName);
        if (a != null) {
            a.getValue();
        }
        return null;
    }

    /**
     * Gets the DOM attribute of <code>element</code> with the given name.
     * It returns the first attribute that matches the name, regardless the case.
     * This is particularly useful when dealing with an HTML DOM, as HTML is not
     * case sensitive.
     * @param element the source element
     * @param attributeName the attribute name
     * @return the XML attribute if any, null otherwise
     */
    public static Attr getAttributeIgnoreCase(Element element, String attributeName) {
        NamedNodeMap nm = element.getAttributes();
        for (int i = 0; i < nm.getLength(); i++) {
            Attr a = (Attr) nm.item(i);
            if (a.getName().equalsIgnoreCase(attributeName)) {
                return a;
            }
        }
        return null;
    }
}

Related

  1. getAttributeValueByName(NamedNodeMap nnm, String name)
  2. getAttributeValueByName(NamedNodeMap nnm, String name)
  3. getAttributeValueByName(Node node, String name)
  4. getAttributeValueByName(Node node, String name)
  5. getAttributeValueEmptyNull(Element e, String attributeName)
  6. getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI, @Nonnull final String sAttrName, @Nullable final String sDefault)
  7. getAttributeValueNS(Element element, String namespace, String attrName)
  8. getAttributeValueOrNull(NamedNodeMap attributes, String attributeName)
  9. getAttributeValuePair(Node node)