
- 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 setDocType() Method
The Java JDOM setDocType() method of Document class is used to set the DocType definition for an XML document. The DocType definition(DTD) is used to set the rules for an XML document such as number of elements inside root, data types of text content of various elements, order of child elements etc.
We can define the DTD inside an XML document which is internal DTD or link the external DTD file to our XML document which is external DTD. Using the setDocType() method, we can set internal DTD and external DTD to the XML document.
Syntax
Following is the syntax of the Java JDOM Document setDocType() method −
Document.setDocType(docType);
Parameters
The Java setDocType() method accepts a single parameter.
docType − the DocType object that we need to set.
Return Value
The Java setDocType() method returns the modified document after setting the DTD(DocType definition).
Example 1
Here is the basic example of using the Java JDOM Document setDocType() method −
import org.jdom2.DocType; import org.jdom2.Document; public class SetDocType { public static void main(String args[]) { try { //Creating new document Document doc = new Document(); //Create and set DocType DocType docType = new DocType("root"); doc.setDocType(docType); System.out.println(doc); } catch(Exception e) { e.printStackTrace(); } } }
The document after adding the DocType definition is displayed.
[Document: [DocType: <!DOCTYPE root>], No root element]
Example 2
The setInternalSubset() method of DocType class is used to set the data inside the internal DTD. After setting the data, the DocType object is passed as an argument to setDocType() method to set the DTD.
import org.jdom2.DocType; import org.jdom2.Document; public class SetInternalDTD { public static void main(String args[]) { try { //Creating new document Document doc = new Document(); //Create and add data to DocType DocType docType = new DocType("root"); docType.setInternalSubset("<!ELEMENT root (child+)> <!ELEMENT child (#PCDATA)>"); //Set DocType to document doc.setDocType(docType); System.out.println(doc); } catch(Exception e) { e.printStackTrace(); } } }
The output window displays the DTD along with the data.
[Document: [DocType: <!DOCTYPE root [ <!ELEMENT root (child+)> <!ELEMENT child (#PCDATA)>]>], No root element]
Example 3
The external DTD file can be passed as a second argument to the DocType() constructor while creating the DocType object. The following program sets external DTD file to the XML document −
import org.jdom2.DocType; import org.jdom2.Document; public class SetExternalDTD { public static void main(String args[]) { try { //Creating new document Document doc = new Document(); //Set external DTD DocType docType = new DocType("root","personInfo.dtd"); doc.setDocType(docType); System.out.println(doc); } catch(Exception e) { e.printStackTrace(); } } }
The DocType declaration of the XML document is displayed.
[Document: [DocType: <!DOCTYPE root SYSTEM "personInfo.dtd">], No root element]