Java JDOM Element setContent() Method



The Java JDOM setContent() method of Element class is used to set the content inside an XML element. Using this method, we can add new content by replacing the already existing content. We can add a single object or a list of content objects using this method.

The setContent() method clears the entire content of an XML element when an empty content list is passed as an argument. In case of an exception, the content list will not be added to the element and the new content list is unaltered.

Syntax

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

Element.setContent(child);
Element.setContent(index, child);
Element.setContent(contentList);
Element.setContent(index, contentList);

Parameters

The Java Element.setContent() method is a polymorphic method and it accepts the following parameters −

  • child − represents a child Content object.
  • index − represents the index at which the content object needs to be set.
  • contentList − represents the list of content objects.

Return Value

The Java setContent() method returns the element to which the content gets set.

Example 1

Here is the basic example of using the Java JDOM Element setContent() method −

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

public class setChildElement {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Create a child element
	     Element child = new Element("child");
	     //Set child element
	     root.setContent(child);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The updated XML document after adding the child element gets displayed.

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <child />
</root>

Example 2

Using the setContent() method, we can set the list of content objects at a time by passing the content list as a parameter to it.

import org.jdom2.Document;
import org.jdom2.Element;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Content;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class setChildElement {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Create a child elements
	     Element child1 = new Element("child1");
	     Element child2 = new Element("child2");
	     List<Content> contentList = new ArrayList<Content>();
	     contentList.add(child1);
	     contentList.add(child2);
	     //Set content list to root
	     root.setContent(contentList);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The updated XML document after adding the child element list gets displayed.

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <child1 />
  <child2 />
</root>

Example 3

We need to parse the following sample.xml file −

<library> Readers choice
   <book>War and peace</book>
   <book>Random thoughts</book>
</library>

The setContent() method replaces the content object at the provided index with the supplied content object.

import java.io.File;

import org.jdom2.Comment;
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 ReplaceContent {
   public static void main(String args[]) {
      try {	
    	 //Reading the document and get root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("src/setContent/sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Create comment
	     Comment comment = new Comment("Book information is available here");
	     //Add comment at index 0
	     root.setContent(1,comment);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML document after replacing the content object is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<library>
  Readers choice
  <!--Book information is available here-->
  <book>Random thoughts</book>
</library>
Advertisements