Java JDOM Document getDocument() Method



The Java JDOM getDocument() method of Document class is used to get one more instance of the current document.

Syntax

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

Document.getDocument();

Parameters

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

Return Value

The Java getDocument() method returns one more instance of the current document in the form of Document object.

Example 1

The following basic example uses the Java JDOM Document getDocument() method to get the copy of an XML document −

import org.jdom2.Document;

public class GetDocument {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document 
	     Document doc = new Document();
	     //Get Document instance
	     Document doc1 = doc.getDocument();
	     System.out.println("----Original Document----\n"+doc);
	     System.out.println("\n----Document instance----\n"+doc1);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The original document and the copy of the document are displayed.

----Original Document----
[Document:  No DOCTYPE declaration,  No root element]

----Document instance----
[Document:  No DOCTYPE declaration,  No root element]

Example 2

The following example adds root element to the original document and uses getDocument() method to get the copy of the document and this copy contains the added root element.

import org.jdom2.Document;
import org.jdom2.Element;

public class GetDocument {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document and add root 
	     Document doc = new Document();
	     Element element = new Element("company").setText("ABC store");
	     doc.addContent(element);
	     //Get Document instance
	     Document doc1 = doc.getDocument();
	     System.out.println("----Original Document----\n"+doc);
	     System.out.println("\n----Document instance----\n"+doc1);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The original document and the copy of the document are displayed.

----Original Document----
[Document:  No DOCTYPE declaration,  No root element]

----Document instance----
[Document:  No DOCTYPE declaration,  No root element]
Advertisements