Java JDOM Document setRootElement() Method



The Java JDOM setRootElement() method of Document class is used to set the root element of an XML document. This method can be used to set the root to any empty document. If this method is used on a document that already has a root, then the root element is replaced.

Syntax

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

Document.setRootElement(rootElement);

Parameters

The Java setRootElement() method accepts a single parameter.

rootElement − This represents an Element object that we need to set as a root element.

Return Value

The Java setRootElement() method returns the modified document after setting the new root element.

Example 1

The Java setRootElement(root) method is used in the following java program on an empty JDOM document. Firstly, an Element object is created with the name, 'company' and then the setRootElement() method is used to set it as root element.

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;

public class SetRootElement {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create root Element
	     Element root = new Element("company").setText("xyz");
	     //Add root Element
	     doc.setRootElement(root);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
         xmlOutput.output(doc, System.out);        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the XML document after setting the root element.

<?xml version="1.0" encoding="UTF-8"?>
<company>xyz</company>

Example 2

Here is the EmpData.xml file for which we need to set the new root −

<Company>
   <Employee>John</Employee>
   <Employee>Daniel</Employee>
</Company>

The setRootElement(root) method is used to set the new root element to the already existing EmpData.xml file. This method completely replaces the root and its child elements with the new root element.

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 SetRootElement {
   public static void main(String args[]) {
      try {	
    	 //Reading the XML file
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("EmpData.xml");			          
 		 Document doc = saxBuilder.build(inputFile);		 
 		 //Print original document 
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("------Document BEFORE setting the Root-------");
         xmlOutput.output(doc, System.out);
	     //Create root Element
	     Element root = new Element("college").setText("xyz");
	     //Add root Element
	     doc.setRootElement(root);
	     //Print document after setting root
	     System.out.println("\n"+"------Document AFTER setting the Root-------");
         xmlOutput.output(doc, System.out);        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the content of the document before and after setting the root element.

------Document BEFORE setting the Root-------
<?xml version="1.0" encoding="UTF-8"?>
<Company>
  <Employee>John</Employee>
  <Employee>Daniel</Employee>
</Company>

------Document AFTER setting the Root-------
<?xml version="1.0" encoding="UTF-8"?>
<college>xyz</college>
Advertisements