Here you can find the source of removeNodeContents(Node aSource, boolean aRemoveAttrs)
Parameter | Description |
---|---|
aSource | the source of the remove operation. |
aRemoveAttrs | True if attributes are to be removed False otherwise |
public static void removeNodeContents(Node aSource, boolean aRemoveAttrs)
//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); } }