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/>]
Advertisements