Java JDOM Document getRootElement() Method



The Java JDOM Document getRootElement() method of Document class retrieves the root element of an XML document.

Syntax

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

Document.getRootElement();

Parameters

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

Return Value

The Java getRootElement() method returns the root element in the form of an Element object.

Example 1

Here is the basic example that explains the usage of the Java JDOM Document getRootElement() method −

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

public class GetRootElement {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document and adding the root
	     Document doc = new Document();
	     Element element = new Element("company");
	     doc.addContent(element);
	     //Getting the root
	     Element root = doc.getRootElement();
	     System.out.println(root);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the name of the root element.

[Element: <company/>]

Example 2

The getRootElement() method throws IllegalStateException when we are trying to get the root element of a document that has no root element.

The following example illustrates this scenario −

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

public class GetRootElement {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document
	     Document doc = new Document();
	     //Getting the root
	     Element root = doc.getRootElement();
	     System.out.println(root);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The above programs throws the following exception −

java.lang.IllegalStateException: Root element not set
	at org.jdom2.Document.getRootElement(Document.java:220)
	at getRootElement.Example2.main(Example2.java:12)

Example 3

The getRootElement() method only retrieves the root element from the document. It will not delete the root as detachRootElement() method.

The bookstore.xml file from which we want to get the root −

<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of a Bookstore (last updated 05-08-2024) -->
<bookstore>
	<book>Treasure Island</book>
	<book>War and Peace</book>
</bookstore>

The following program uses the getRootElement() method to retrieve the root and also prints the document to see whether the root is preserved inside the document.

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class GetRootElement {
   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);		 
 		 //Getting the root
	     Element root = doc.getRootElement();
	     System.out.println(root);
	     //Printing the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("\n--------XML document after getting the root--------\n");
         xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the root element and also the entire document after using the method.

[Element: <bookstore/>]

--------XML document after getting the root--------

<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of a Bookstore (last updated 05-08-2024) -->
<bookstore>
  <book>Treasure Island</book>
  <book>War and Peace</book>
</bookstore>
Advertisements