Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Xml Dom Remove Element from XML Document

Related Tutorials


 
 

Share on Google+Share on Google+

Remove Element from XML Document

Advertisement
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>
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: June 12, 2007

Related Tutorials

Discuss: Remove Element from XML Document   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
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 removeelement.java 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(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14 2) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472) C:\Program Files\Java\jdk1.7.0\bin>
Derik
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?
DMCA.com