Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Vector; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static Document removeFromRootElement(Document document, String nodeName, String attributeName, String attributeValue) { try { Element rootElement = document.getDocumentElement(); NodeList nl = document.getElementsByTagName(nodeName); Vector<Node> deletedNodes = new Vector<Node>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); String noteAttributeValue = node.getAttributes().getNamedItem(attributeName).getNodeValue(); if (noteAttributeValue.equals(attributeValue)) { deletedNodes.add(node); } } for (Node deletedNode : deletedNodes) { rootElement.removeChild(deletedNode); } } catch (Exception e) { e.printStackTrace(); } return document; } }