Share on Google+Share on Google+

Remove Element from XML Document

In this section, you will learn to remove any element from a given XML document. Whenever you remove the xml element from the xml document the data are also lost from the xml element.

Remove Element from XML Document


In this section, you will learn to remove any element from a given  XML document. Whenever you remove the xml element from the xml document the data are also lost from the xml element.

Description of program:

This program takes a XML file name on the console. At the run time the program asks for an element name to be deleted. The removeChild() method  removes the given element from the XML document by  invoking the getParentNode() method .

Here is the XML File:E-mail.xml

<?xml version="1.0"?>
<Body>Be ready for a cruise...</Body>

Here is the Java File:

import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 

public class RemoveElement {
  static public void main(String[] arg) {
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  System.out.print("Enter a XML file name: ");
  String xmlFile = bf.readLine();
  File file = new File(xmlFile);
  System.out.print("Enter an element which have to delete: ");
  String remElement = bf.readLine();
  if (file.exists()){
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlFile);
  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer tFormer = tFactory.newTransformer();
  Element element = (Element)doc.getElementsByTagName(remElement).item(0);
//  Remove the node
//  Normalize the DOM tree to combine all adjacent nodes
  Source source = new DOMSource(doc);
  Result dest = new StreamResult(System.out);
  tFormer.transform(source, dest);
  System.out.println("File not found!");
  catch (Exception e){

Download this example.

Output of this program:


C:\vinod\xml>java RemoveElement
Enter a XML file name: E-mail.xml
Enter an element which have to delete: Subject
<?xml version="1.0" encoding="UTF-8" standalone="no"?><E-mail>

<Body>Be ready for a cruise...</Body>



Posted on: June 12, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Remove Element from XML Document   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
blankedy blanks
September 7, 2011
this doesn't work

this doesn't work. please give exact instructions as to how to get this to work. for example, should i start by using a computer, or a pumpkin? and if it's a computer, does it matter if i'm using windows or linux or something else? and from there, do i need a jdk? perhaps? and then from there, how?!?! i've searched the internet far and wide on how in the world to remove an element from xml, and all i've found are super-specific tid-bits that (i suspect) depend on a huge grand scheme of things that nobody is telling me. the w3schools example doesn't work. nothing works. i've tried xslt, javascript, and even a regex find/replace, which was pretty close but no cigar: i want to delete <f> nodes. my search term in notepad++ using regular expressions: <f.*</f> this identifies most nodes fine, but when there's 2 f nodes in 1 line then it highlights both of them, and includes data that's NOT in an f node.
blankedy blanks
September 7, 2011
specific errors

C:\Program Files\Java\jdk1.7.0\bin>javac C:\Program Files\Java\jdk1.7.0\bin>java removeelement Exception in thread "main" java.lang.NoClassDefFoundError: removeelement (wrong name: RemoveElement) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass( at 2) at at$100( at$ at$ at Method) at at java.lang.ClassLoader.loadClass( at sun.misc.Launcher$AppClassLoader.loadClass( at java.lang.ClassLoader.loadClass( at sun.launcher.LauncherHelper.checkAndLoadMain( C:\Program Files\Java\jdk1.7.0\bin>
August 16, 2012
Why does noone indent their code?

I see thousands of code posts weekly where noone bothers to indent, or the web portal doesn't support code indenting. Do you really think people want to look at this unindented garbage?