Share on Google+Share on Google+

Cloning a XML Element

In this section, you will learn to create a clone of a element in the DOM tree. In general, the cloning means to create a duplicate.

Cloning a XML Element


In this section, you will learn to create a clone of a  element in the DOM tree. In general, the cloning means to create a duplicate. 

Description of a program:

The following program helps you in creating a clone of any element of the specified XML file. For creating a DOM object ,  you need the  DocumentBuilderFactoty and the DocumentBuilder objects. After parsing it displays a xml file on the console using the transform() method. At  run time the program asks for a  element name to clone . Here the element1.cloneNode(true) method creates a clone and element1.getParentNode().insertBefore(copyElement, element1.getNextSibling()) inserts the clone element at the specified position.

Here is the XML File: Employee-Detail2.xml

<?xml version = "1.0" ?>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> </Emp_E-mail>

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 DOMCloneElements {
  static public void main(String[] arg){
  try  {
  BufferedReader bf = new BufferedReader(
  System.out.print("Enter XML file name: ");
  String xmlFile = bf.readLine();
  File file = new File(xmlFile);
  if (file.exists()){
  DocumentBuilderFactory factory = 
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlFile);
  TransformerFactory tFactory = 
  Transformer tformer = tFactory.newTransformer();
  Source source = new DOMSource(doc);
  Result result = new StreamResult(System.out);
  System.out.println(xmlFile + " file: ");
  tformer.transform(source, result);
  System.out.print("Enter the element to clone: ");
  String clone = bf.readLine();
  System.out.print("Enter data to add: ");
  String addElement = bf.readLine();
  NodeList list = doc.getElementsByTagName
  Element element1 = (Element)list.item(0);
  Element copyElement = (Elementelement1.cloneNode(true);
  tformer.transform(source, result);
  System.out.println("File not found!");
  catch (Exception e){

Download this example.

 Output of this program:


C:\vinod\xml>java DOMCloneElements
Enter XML file name: Employee-Detail2.xml
Employee-Detail2.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Employee-Detail>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> </Emp_E-mail>
Enter the element to clone: Emp_Id
Enter data to add: E011
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Employee-Detail>
<Emp_Id> E-001 E011</Emp_Id><Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> </Emp_E-mail>



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: Cloning a XML Element  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
November 30, 2011

This program writes contents to console . ho to make to write into xml?