Home Xml Dom Remove Element from XML Document



Remove Element from XML Document
Posted on: June 12, 2007 at 12:00 AM
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"?>
<E-mail>
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...</Body>
</E-mail>

Here is the Java File: RemoveElement.java

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 RemoveElement {
  static public void main(String[] arg) {
  try{
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  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
  element.getParentNode().removeChild(element);
//  Normalize the DOM tree to combine all adjacent nodes
  doc.normalize();
  Source source = new DOMSource(doc);
  Result dest = new StreamResult(System.out);
  tFormer.transform(source, dest);
  System.out.println();
  }
  else{
  System.out.println("File not found!");
  }
  }
  catch (Exception e){
  System.err.println(e);
  System.exit(0);
  }
  }
}

Download this example.

Output of this program:

C:\vinod\xml>javac RemoveElement.java

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>
<To>Rohan</To>
<From>Amit</From>

<Body>Be ready for a cruise...</Body>
</E-mail>

Related Tags for Remove Element from XML Document:
cxmlfiletimeconsoledeletemethodgetremovenameolethisnodeelementmoveparentforchildprogramtoramrunldeildocimfrominnomlasmntparletesemmeprodossodelatkisimeivarconsxmssrenthchiinvokingprmovodeonomogrolo


More Tutorials from this section

Ask Questions?    Discuss: Remove Element from XML Document   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.