Java XML Child Remove removeChildElements(Element parent)

Here you can find the source of removeChildElements(Element parent)

Description

removes all children of element type

License

Common Public License

Declaration

public static void removeChildElements(Element parent) 

Method Source Code


//package com.java2s;
/*/*from   w  ww.java2 s  .  com*/
 * ====================================================================
 * This software is subject to the terms of the Common Public License
 * Agreement, available at the following URL:
 *   http://www.opensource.org/licenses/cpl.html .
 * Copyright (C) 2003-2004 TONBELLER AG.
 * All Rights Reserved.
 * You must accept the terms of that agreement to use this software.
 * ====================================================================
 *
 * 
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

public class Main {
    /**
     * removes all children of element type
     */
    public static void removeChildElements(Element parent) {
        List v = getChildElements(parent);
        Iterator en = v.iterator();
        while (en.hasNext())
            parent.removeChild((Node) en.next());
    }

    /**
     * returns List of all direct child elements
     */
    public static List getChildElements(Node parent) {
        ArrayList retVec = new ArrayList();

        NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); ++i) {
            if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
                retVec.add(children.item(i));
            }
        }
        return retVec;
    }
}

Related

  1. removeChild(Element parent, String name)
  2. removeChild(final Node node, final Node child)
  3. removeChild(Node xmlNode, String name, boolean ignoreCase)
  4. removeChild(NodeList nodes)
  5. removeChildNodes(Node node)
  6. removeChildNodes(Node node)
  7. removeChildNodes(Node node, short... ignoreNodeTypes)
  8. removeChildNodes(Node parent)