
- 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 Class
The Java JDOM Document class represents an XML Document. This class provides various methods to access root element, DocType Definition (DTD), XML namespaces and document level Content objects.
The package org.jdom2 contains the Document class. This class implements the Parent interface and inherits java.lang.Object class. The Document class considers the entire content inside the root element as a single content object.
Using the methods of Document class, we can add, modify, delete Content objects inside an XML Document. Content objects include processing instruction, DocType definition, comments and root element.
Constructors of Document Class
To create a Document object, the Document class provides the following default and parameterized constructors −
- Document() − default constructor to create an empty document.
- Document(Element rootElement) − creates a new Document object with specified root element.
- Document(Element rootElement, DocType docType) − creates a new Document with specified root element and DocType definition.
- Document(Element rootElement, DocType docType, java.lang.String baseURI) − creates a new Document with specified root element, DocType definition and BaseURI.
- Document(java.util.List<? extends Content> content) − creates a new Document with the Content objects of the supplied list.
Creating an XML Document
Using the above mentioned constructors, we can create XML documents. Here is a Java program that uses a default constructor and a parameterized constructor to create two XML documents.
import org.jdom2.Document; import org.jdom2.Element; public class CreateDocument { public static void main(String args[]) { try { //Using default constructor Document doc1 = new Document(); System.out.println(doc1); //Using parameterized constructor Document doc2 = new Document(new Element("book")); System.out.println(doc2); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the two documents created using constructors.
[Document: No DOCTYPE declaration, No root element] [Document: No DOCTYPE declaration, Root is [Element: <book/>]]
Operations on Root Element
The root element is the main component of any XML document without which we cannot insert any other information of elements inside an XML document. The Document class provides the following methods to perform operations on root element.
Method | Description |
---|---|
getRootElement() | Retrieves root element of an XML document. |
detachRootElement() | Detaches root element of an XML document. |
hasRootElement() | Checks if there is root inside an XML document. |
setRootElement() | Sets or updates the root for an XML document. |
Retrieve XML Content
To retrieve document level information from an XML document, the Document class provides the following methods −
Method | Description |
---|---|
getDocType() | Returns the DocType declaration of an XML document. |
getBaseURI() | Returns the BaseURI of an XML document. |
getContentSize() | Returns the number of content objects inside an XML document. |
getContent() | Returns the content objects of the document. |
indexOf(Content child) | Returns the index of a Content object. |
cloneContent() | Returns the list of content objects of an XML document. |
getDescendants() | Returns all the descendants of an XML document. |
Add and Delete XML Content
To add and remove Content objects from an XML document, the Document class has these methods −
Method | Description |
---|---|
addContent() | Adds Content objects to an XML document. |
removeContent() | Removes content objects from an XML document. |
Update XML Content
The Document class provides the following methods to modify content inside an XML document. These methods replaces the content objects if already present, else they insert new content objects.
Method | Description |
---|---|
setBaseURI() | Sets or updates BaseURI of an XML document. |
setDocType() | Sets or updates DocType of an XML document. |
setContent() | Sets or updates Content objects of an XML document. |
Get XML Namespaces
All the namespaces used inside an XML document can be obtained using the following methods of Document class −
Method | Description |
---|---|
getNamespacesInherited() | Returns the list of all inherited XML namespaces that are not introduced in the current XML dcoument. |
getNamespacesInScope() | Returns the list of all XML namespaces that are in-scope of this document. |
getNamespacesIntroduced() | Returns the list of all XML namespaces that are introduced in the current document. |
Get Copies of XML Document
To create more instances of an existing XML document, the Document class provides the following methods −
Method | Description |
---|---|
getDocument() | Returns an instance of the current XML document. |
clone() | Returns a deep clone of an XML document. |
Other Methods
Following are few more methods of Document class that has different functionality −
Method | Description |
---|---|
toString() | Returns an XML document as a string. |
hashCode() | Returns the hash code for an XML document. |