Java JDOM Document clone() Method



The Java JDOM clone() method of Document class is used to get the deep clone of a document. It creates the exact copy of the current document.

Syntax

Following is the syntax of the Java JDOM Document clone() method −

Document.clone();

Parameters

The Java clone() method doesn't accept any parameters.

Return Value

The Java clone() method returns copy of the current Document object.

Example 1

In this basic example, we have created an XML document and used the Java JDOM Document clone() method to get the exact copy of it.

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;

public class CloneDocument {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create root Element
	     Element root = new Element("bookstore").setText("Coffee with Pages");
	     //Add root Element
	     doc.addContent(root);
	     //Cloning the document
	     Document cloneDoc = doc.clone();
	     //Printing document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     System.out.println("-----------Original Document-----------\n");
	     xmlOutput.output(doc, System.out); 
	     System.out.println("\n-----------Cloned Document-----------\n");
         xmlOutput.output(cloneDoc, System.out);        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the original document and the cloned document.

-----------Original Document-----------

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>Coffee with Pages</bookstore>

-----------Cloned Document-----------

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>Coffee with Pages</bookstore>

Example 2

We need to clone the following bookstore.xml file −

<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of a Bookstore (last updated o5-08-2024) -->
<bookstore>
	<!-- All the books in the store are listed -->
   <book>
      <name>Treasure Island</name>
	  <author>Robert Louis</author>
	  <price>1400</price>
   </book>
   <book>
      <name>Oliver Twist</name>
	  <author>Charles Dickens</author>
	  <price>2000</price>
   </book>
   <book>
      <name>War and Peace</name>
	  <author>Leo Tolstoy</author>
	  <price>1500</price>
   </book>
</bookstore>

Let us read an already existing bookstore.xml file and get the clone of this document. To do so, we have first read the bookstore.xml file and build the document using SAXBuilder. Then, we have used the clone() method to get the deep clone of this document.

import java.io.File;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class CloneDocument {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("bookstore.xml");
 		 Document doc = saxBuilder.build(inputFile);		 
	     //Cloning the document
	     Document cloneDoc = doc.clone();
	     //Printing document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("-----------Original Document-----------\n");	     
	     xmlOutput.output(doc, System.out); 
	     System.out.println("\n-----------Cloned Document-----------\n");
         xmlOutput.output(cloneDoc, System.out);        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the original bookstore.xml file and the cloned document of this file.

-----------Original Document-----------

<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of a Bookstore (last updated o5-08-2024) -->
<bookstore>
  <!-- All the books in the store are listed -->
  <book>
    <name>Treasure Island</name>
    <author>Robert Louis</author>
    <price>1400</price>
  </book>
  <book>
    <name>Oliver Twist</name>
    <author>Charles Dickens</author>
    <price>2000</price>
  </book>
  <book>
    <name>War and Peace</name>
    <author>Leo Tolstoy</author>
    <price>1500</price>
  </book>
</bookstore>

-----------Cloned Document-----------

<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of a Bookstore (last updated o5-08-2024) -->
<bookstore>
  <!-- All the books in the store are listed -->
  <book>
    <name>Treasure Island</name>
    <author>Robert Louis</author>
    <price>1400</price>
  </book>
  <book>
    <name>Oliver Twist</name>
    <author>Charles Dickens</author>
    <price>2000</price>
  </book>
  <book>
    <name>War and Peace</name>
    <author>Leo Tolstoy</author>
    <price>1500</price>
  </book>
</bookstore>
Advertisements