remove All XML Children - Java XML

Java examples for XML:XML Element Child

Description

remove All XML Children

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

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

public class Main {

    public static void removeAllChildren(Node node) {
        removeAllChilderenWithoutHeader(node, 0);
    }/*w ww  . ja v  a  2 s  .c  o  m*/

    public static void removeAllChilderenWithoutHeader(Node node,
            int remainChildCount) {
        NodeList childNodes = node.getChildNodes();
        List<Node> removeNodeList = new ArrayList<Node>();
        for (int i = remainChildCount; i < childNodes.getLength(); i++) {
            removeNodeList.add(childNodes.item(i));
        }

        for (Node childNode : removeNodeList) {
            node.removeChild(childNode);
        }

    }
}

Related Tutorials