Here you can find the source of removeElements(Element from, String named)
Parameter | Description |
---|---|
from | which to remove items named named |
named | is the name of the elements to remove from from |
public static void removeElements(Element from, String named)
//package com.java2s; //License from project: Apache License import org.w3c.dom.*; import java.util.ArrayList; import java.util.List; public class Main { /**/*from www. j av a 2 s .c o m*/ * Removes all children named {@code named} from element {@code from} * @param from which to remove items named {@code named} * @param named is the name of the elements to remove from {@code from} */ public static void removeElements(Element from, String named) { NodeList children = from.getChildNodes(); boolean removeNextNewline = false; List<Node> removes = new ArrayList<Node>(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if ((child != null) && named.equals(child.getNodeName())) { removes.add(child); removeNextNewline = true; } else if (removeNextNewline && (child != null) && (child instanceof Text) && child.getNodeValue().trim().isEmpty()) { removes.add(child); } else { removeNextNewline = false; } } for (Node node : removes) { from.removeChild(node); } } }