Java JDOM Document setContent() Method



The Java JDOM setContent() method of Document class is used to replace document level content objects inside an XML document. The content objects include DocType definition, comments, processing instruction and root element.

The setContent() method throws an indexOutOfBoundsException when we are trying to replace a content object at an incorrect index inside the document.

Syntax

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

Document.setContent(child)
Document.setContent(index, child)
Document.setContent(list)
Document.setContent(index, list)

Parameters

The Java setContent() method is a polymorphic method and has different parameters for each of them.

  • child − represents a Content object. This can be an Element, Comment, DocType, EntityRef, ProcessingInstruction and Text.

  • index − represents the index at which we need to insert the Content or list of Content objects.

  • list − represents the list of Content objects.

Return Value

This method returns the Document on which the content gets replaced.

Example 1

The Java JDOM Document setContent() method replaces the existing root element with the new root element that is passed as an argument to this method.

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

public class setNewRoot {
   public static void main(String args[]) {
      try {
    	 //Creating new document and adding root
    	 Document doc = new Document();
    	 Element root = new Element("root");
    	 doc.addContent(root);
    	 //Replacing the root
    	 Element new_root = new Element("new_root");
    	 doc.setContent(new_root);
    	 System.out.println(doc);
      } catch(Exception e) {
    	 e.printStackTrace();
      } 
   }
}

The updated document after setting the new root is displayed.

[Document:  No DOCTYPE declaration, Root is [Element: <new_root/>]]

Example 2

We need to replace the comment of the following collegeData.xml file −

<!-- This is college data.-->
<college>
   <department>Computer Science</department>
   <department>Electrical and Electronics</department>
   <department>Mechanical</department>
</college>

The setContent(index,comment) method replaces the content object at the specified index with the comment that is passed as the second argument.

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

public class ReplaceComment {
   public static void main(String args[]) {
      try {
    	 //Reading the document
   		 SAXBuilder saxBuilder = new SAXBuilder();
   		 File inputFile = new File("collegeData.xml");
   		 Document doc = saxBuilder.build(inputFile);
   		 //Replacing comment
    	 Comment comment = new Comment("Start of the document");    	 
    	 doc.setContent(0,comment);
    	 //Printing the document
    	 XMLOutputter xmlOutput = new XMLOutputter();
    	 xmlOutput.setFormat(Format.getPrettyFormat());
    	 xmlOutput.output(doc, System.out);
      } catch(Exception e) {
    	 e.printStackTrace();
      } 
   }
}

The updated document after replacing the comment has been displayed.

<?xml version="1.0" encoding="UTF-8"?>
<!--Start of the document-->
<college>
  <department>Computer Science</department>
  <department>Electrical and Electronics</department>
  <department>Mechanical</department>
</college>

Example 3

The setContent(index,list) method replaces the content object at the specified index with the content list that is supplied as the second argument.

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetContentList {
   public static void main(String args[]) {
      try {
     	 //Reading the document
  		 SAXBuilder saxBuilder = new SAXBuilder();
  		 File inputFile = new File("collegeData.xml");
  		 Document doc = saxBuilder.build(inputFile);
  		 //Adding content list
  		 DocType docType = new DocType("college");
  		 Comment comment = new Comment("Last updated date : 09-08-2024");
  		 List<Content> list = new ArrayList<Content>();
  		 list.add(docType);
  		 list.add(comment);
    	 doc.setContent(0,list);
    	 //Printing the document
    	 XMLOutputter xmlOutput = new XMLOutputter();
    	 xmlOutput.setFormat(Format.getPrettyFormat());
    	 xmlOutput.output(doc, System.out);
      } catch(Exception e) {
    	 e.printStackTrace();
      } 
   }
}

The updated document after adding the content list at index 0 is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE college>
<!--Last updated date : 09-08-2024-->
<college>
  <department>Computer Science</department>
  <department>Electrical and Electronics</department>
  <department>Mechanical</department>
</college>
Advertisements