Java XML Child Remove All removeAllChildren(Element node, String elementName)

Here you can find the source of removeAllChildren(Element node, String elementName)

Description

Delete children elements for Element by element name

License

Open Source License

Declaration

public static void removeAllChildren(Element node, String elementName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*from  w  ww  .jav a2 s.c o  m*/
     * Delete children elements for Node
     */
    public static void removeAllChildren(Node node) {
        if (node != null) {
            while (node.hasChildNodes()) {
                node.removeChild(node.getFirstChild());
            }
        }
    }

    /**
     * Delete children elements for Element by element name
     */
    public static void removeAllChildren(Element node, String elementName) {
        if (node != null) {
            NodeList nl = node.getChildNodes();
            int len = nl.getLength();
            for (int i = 0; i < len; i++) {
                Node childNode = nl.item(i);
                if (childNode != null && childNode.getLocalName() != null
                        && childNode.getLocalName().equals(elementName))
                    node.removeChild(childNode);
            }
        }
    }
}

Related

  1. removeAllChildNodes(Node node)
  2. removeAllChildNodes(Node node)
  3. removeAllChildNodes(Node node)
  4. removeAllChildNodes(Node node, String name)
  5. removeAllChildren(Element deps)
  6. removeAllChildren(final Element element)
  7. removeAllChildren(final Node node)
  8. removeAllChildren(Node node)
  9. removeAllChildren(Node node)