
- 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 cloneContent() Method
The Java JDOM cloneContent() method of Document class is used to get the content of an XML document in the form of a list. This list contains all the items in the form of Content objects. These Content objects are comments, root element and XML DTD.
Syntax
Following is the syntax of the Java JDOM Document cloneContent() method −
Document.cloneContent();
Parameters
The Java cloneContent() method doesn't accept any parameters.
Return Value
The Java cloneContent() method returns a List of Content objects.
Example 1
Here is the basic example of using Java JDOM Document cloneContent() method −
import java.util.List; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; public class CloneContent { 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 content List<Content> clonedList = doc.cloneContent(); //Printing the list items for(Content c : clonedList) { System.out.println(c); } } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the name of the root element.
[Element: <bookstore/>]
Example 2
We need to parse the following bookstore.xml file −
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE bookstore[ <!ELEMENT bookstore (book+)> <!ELEMENT book (name,author,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT price (#PCDATA)> ]> <!-- Information of a Bookstore (last updated 05-08-2024) --> <!-- All the books in the store are listed --> <bookstore> <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>
Now, let us read the above bookstore.xml file and get the list of Content objects using cloneContent() method.
import java.io.File; import java.util.List; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.input.SAXBuilder; public class CloneContent { 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 content List<Content> clonedList = doc.cloneContent(); //Printing the list items for(Content c : clonedList) { System.out.println(c); } } catch (Exception e) { e.printStackTrace(); } } }
All the four Content objects of the XML document are printed.
[DocType: <!DOCTYPE bookstore>] [Comment: <!-- Information of a Bookstore (last updated 05-08-2024) -->] [Comment: <!-- All the books in the store are listed -->] [Element: <bookstore/>]