Java XML Attribute Remove removeAttributeIgnoreCase(Element element, String attributeName)

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

Description

Removes the DOM attribute from element with the given name.

License

Open Source License

Parameter

Parameter Description
element the source element
attributeName the attribute name

Declaration

public static void removeAttributeIgnoreCase(Element element, String attributeName) 

Method Source Code

//package com.java2s;
/*/*from w w  w .ja va  2 s .  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 {
    /**
     * Removes the DOM attribute from <code>element</code> with the given name.
     * It removes 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
     */
    public static void removeAttributeIgnoreCase(Element element, String attributeName) {
        Attr a = getAttributeIgnoreCase(element, attributeName);
        if (a != null) {
            element.removeAttributeNode(a);
        }
    }

    /**
     * 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. removeAttribute(Node node, String attName)
  2. removeAttribute(Node node, String attr)
  3. removeAttribute(Node node, String attrName)
  4. removeAttribute(Node node, String... attributs)
  5. removeAttribute(Node parent, String name, String value, boolean recursive)
  6. removeAttributes(Element e, String namespaceURI)
  7. removeAttributes(Element elem)
  8. removeAttributes(Element element)
  9. removeAttributes(Element target, boolean flag)