Java XML Attribute Remove removeNodeContents(Node aSource, boolean aRemoveAttrs)

Here you can find the source of removeNodeContents(Node aSource, boolean aRemoveAttrs)

Description

Removes the children and all attributes for the given source node.

License

Open Source License

Parameter

Parameter Description
aSource the source of the remove operation.
aRemoveAttrs True if attributes are to be removed False otherwise

Declaration

public static void removeNodeContents(Node aSource, boolean aRemoveAttrs) 

Method Source Code

//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**/* ww w  .java  2 s.  c  o m*/
     * Removes the children and all attributes for the given source node.
     *
     * @param aSource      the source of the remove operation.
     * @param aRemoveAttrs True if attributes are to be removed False otherwise
     */
    public static void removeNodeContents(Node aSource, boolean aRemoveAttrs) {
        if (aRemoveAttrs && aSource.hasAttributes()) {
            // Remove any attributes the Element may have (Only Elements have attributes)
            NamedNodeMap attrMap = aSource.getAttributes();
            int skip = 0;
            for (int i = 0, len = attrMap.getLength(); i < len; i++) {
                if (attrMap.item(skip).getNodeName().startsWith("xmlns:")) //$NON-NLS-1$
                    skip++;
                else
                    ((Element) aSource).removeAttribute(attrMap.item(skip)
                            .getNodeName());
            }
        }

        // Remove all of the children from the node
        for (Node child = aSource.getFirstChild(); child != null; child = aSource
                .getFirstChild())
            aSource.removeChild(child);
    }
}

Related

  1. removeEmptyAttributes(Element element)
  2. removeInvalidAttributes(Element element, String... validAttributeNames)
  3. removeNodeAttribute(Node node, String attributeName)
  4. removeNodeAttribute(Node node, String name)
  5. removeNodeAttributes(Node node)
  6. removePrefixes(final String xml, final boolean ignoreAttrValues)
  7. removeUnspecifiedIfmapAttributes(Node meta)
  8. removeXmlBaseAttributes(Node node)
  9. removeXmlNsAttribute(final Node node)