Java JDOM Document getDocType() Method



The Java JDOM getDocType() method of Document class is used to get the DocType declaration of an XML document in the form of JDOM DocType object. Some XML documents may not have DocType declaration statements; in that case, it returns null.

To get more information from the DocType object returned by this method, there are methods available in DocType class such as getElementName(), getSystemID(), getPublicID() etc.

Syntax

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

Document.getDocType();

Parameters

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

Return Value

The Java getDocType() method returns the DocType definition in the form of JDOM DocType object.

Example 1

Here is the hospital.xml file that has DocType declaration statements. We are going to use the Java JDOM Document getDocType() method to obtain the DocType declaration.

<!DOCTYPE hospital[
<!ELEMENT hospital (doctor+)>
<!ELEMENT doctor (#PCDATA)>
]>
<hospital>
   <doctor>Dr.Rajesh Sharma</doctor>
   <doctor>Dr.Priya Gowtham</doctor>
   <doctor>Dr.Manoj Varma</doctor>
</hospital>

The following basic Java program uses getDocType() method to retrieve the DocType declaration from the above mentioned hospital.xml file.

import java.io.File;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;

public class GetDocType {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("hospital.xml");
 		 Document doc = saxBuilder.build(inputFile);		 
 		 //Get DocType
	     DocType docType = doc.getDocType();
	     System.out.println(docType);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the DTD declaration.

[DocType: <!DOCTYPE hospital>]

Example 2

The getElementName() method of DocType class retrieves the name of the Element on which the DTD constraints are defined. The getSystemID() and getPublicID() returns the system ID and public ID of the XML document respectively.

import java.io.File;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;

public class GetDocType {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("hospital.xml");
 		 Document doc = saxBuilder.build(inputFile);		 
 		 //Get DocType Element
	     DocType docType = doc.getDocType();
	     System.out.println("DocType Element name: "+docType.getElementName());
	     System.out.println("System ID: "+docType.getSystemID());
	     System.out.println("Public ID: "+docType.getPublicID());
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays DocType Element name, system ID and public ID.

DocType Element name: hospital
System ID: null
Public ID: null

Example 3

The getDocType() method returns null if there is no DocType definition inside the XML document.

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

public class GetDocType {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document and adding the root
	     Document doc = new Document();
	     Element element = new Element("hospital");
	     doc.addContent(element);
	     //Get the DocType
	     DocType docType = doc.getDocType();
	     System.out.println(docType);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the DocType as null.

null
Advertisements