Used to remove a particular node completely along with its value from a XML file
import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; public class Delete { static Transformer tFormer; static DocumentBuilder builder; static Document document; public static void main(String[] args) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); TransformerFactory tFactory = TransformerFactory.newInstance(); try { tFormer = tFactory.newTransformer(); builder = factory.newDocumentBuilder(); document = builder.parse(new File("data.xml")); delNode(document, "LastName"); } catch (Exception e) { e.printStackTrace(); } } public static void delNode(Node parent, String filter) { try { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals(filter)) { parent.removeChild(child); } else { delNode(child, filter); } } } Source source = new DOMSource(document); StreamResult dest = new StreamResult("data.xml"); tFormer.transform(source, dest); } catch (Exception e) { } } }