Java JDOM Document detachRootElement() Method



The Java JDOM detachRootElement() method of Document class detaches and returns the root element from the XML document. Using this method, we can only get the name of the root element. It will not return the text content or any information related to child elements.

This method removes the root element and everything contained inside it. To get the root element without deleting it from the document, we can use getRootElement() method of Document class.

Syntax

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

Document.detachRootElement();

Parameters

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

Return Value

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

Example 1

Here is the basic example of using Java JDOM Document detachRootElement() method −

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

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

The root element name, 'root' is displayed on the output screen.

[Element: <root/>]

Example 2

The detachRootElement() method returns null if there is no root element available in the XML document. The following program tries to detach the root from an empty document and returns null.

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

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

The output window displays null.

null

Example 3

Here is the bookstore.xml file from which we need to detach the root element −

<?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 java program reads the above mentioned bookstore.xml file, builds the document using SAXBuilder and detaches the root element using detachRootElement() method.

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

public class DetachRootElement {
   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);		 
 		 //Detaching the root
	     Element root = doc.detachRootElement();
	     System.out.println(root);
	     //Printing the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     System.out.println("\n--------XML document after detaching the root--------\n");
         xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the detached root and the document after detaching the root.

[Element: <bookstore/>]

--------XML document after detaching the root--------

<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of a Bookstore (last updated 05-08-2024) -->
Advertisements