
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
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>