Java XML Attribute Remove removeAttributes(Element target, boolean flag)

Here you can find the source of removeAttributes(Element target, boolean flag)

Description

Drop the attributes from an element, except possibly an xmlns attribute that declares its namespace.

License

Apache License

Parameter

Parameter Description
target the element whose attributes will be removed.
flag preserve namespace declaration

Declaration

public static void removeAttributes(Element target, boolean flag) 

Method Source Code

//package com.java2s;
/*/*from ww  w.  ja  v a  2  s.  c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 {
    /** The namespaceURI represented by the prefix <code>xmlns</code>. */
    public static final String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";

    /**
     * Drop the attributes from an element, except possibly an <code>xmlns</code>
     * attribute that declares its namespace.
     * @param target the element whose attributes will be removed.
     * @param flag preserve namespace declaration
     */
    public static void removeAttributes(Element target, boolean flag) {
        if (!target.hasAttributes()) {
            return;
        }
        String prefix = target.getPrefix();
        NamedNodeMap nnm = target.getAttributes();
        Attr toPutBack = null;
        if (flag) {
            if (prefix == null) {
                toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns");
            } else {
                toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns:" + prefix);
            }

        }
        while (nnm.getLength() != 0) {
            target.removeAttributeNode((Attr) nnm.item(0));
        }
        if (toPutBack != null) {
            target.setAttributeNodeNS(toPutBack);
        }
    }
}

Related

  1. removeAttribute(Node parent, String name, String value, boolean recursive)
  2. removeAttributeIgnoreCase(Element element, String attributeName)
  3. removeAttributes(Element e, String namespaceURI)
  4. removeAttributes(Element elem)
  5. removeAttributes(Element element)
  6. removeAttributes(SOAPElement elem)
  7. removeDefaultNameSpaceAttributes(final Element element)
  8. removeEmptyAttributes(Element element)
  9. removeInvalidAttributes(Element element, String... validAttributeNames)