Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 {
    /**
     * 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);
            }
        }
    }
}