Java JDOM Element setText() Method



The Java JDOM setText() method of Element class is used to set the text content of an XML element. If text content already exists for the element, it replaces with the supplied text. When null is passed as an argument, an empty string is set as text content. This method throws an IllegalDataException if the supplied text contains an illegal character such as vertical tab.

Syntax

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

Element.setText(text);

Parameters

The Java Element.setText() method accepts a single parameter −

text − represents the text content to be set.

Return Value

The Java setText() method returns the XML Element after updating the text content.

Example 1

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

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

public class SetText {
   public static void main(String args[]) {
      try {	
    	 //Create Element
	     Element element = new Element("book");	
	     //set text
	     element = element.setText("War and peace");
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(element, System.out);     
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The modified XML element after adding text content is displayed.

<book>War and peace</book>

Example 2

When setText() method is used more than once, the text passed in the last call is set as the final text content for the given XML Element.

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

public class SetText {
   public static void main(String args[]) {
      try {	
    	 //Create Element
	     Element element = new Element("book").setText("War and peace");	
	     //set text again
	     element = element.setText("Random thoughts");
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(element, System.out);     
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML element after setting the text is displayed.

<book>Random thoughts</book>

Example 3

When null is passed as an argument to setText() method, it sets an empty text as text content to the element.

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

public class SetText {
   public static void main(String args[]) {
      try {	
    	 //Create Element
	     Element element = new Element("book");	
	     //set text
	     element = element.setText(null);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(element, System.out);     
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML element after setting null as text is displayed.

<book />
Advertisements