Java JDOM Element getTextTrim() Method



The Java JDOM getTextTrim() method of Element class is used to get the text content of an XML element by removing the whitespaces at the beginning and end. It doesn't remove whitespaces inside the text content. This method returns an empty string when there is no text content inside an element.

Syntax

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

Element.getTextTrim();

Parameters

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

Return Value

The Java getTextTrim() method returns the text content of an Element in the form of a string.

Example 1

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

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

public class GetTextContent {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("book").setText("		War and     Peace");
	     doc.setRootElement(root);
	     //Get text content
	     String textContent = root.getTextTrim();
	     System.out.println("Text Content: "+textContent);
	     
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The trimmed text content is displayed on the output screen.

Text Content: War and     Peace

Example 2

The Java getTextTrim() method returns an empty string when there is no text content available inside the XML element.

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

public class GetTextContent {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Get text content
	     String textContent = root.getTextTrim();
	     System.out.println("Text Content: "+textContent);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

An empty string is displayed as text content.

Text Content:

Example 3

The following htmlTable.xml file has CDATA section. We need to parse this XML file.

<htmlTable>
<![CDATA[         <table>]]> - HTML table tag.
</htmlTable>

The Java getTextTrim() method returns CDATA section if available inside the XML element.

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

public class GetTextContent {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("htmlTable.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     //Get the root
	     Element root = doc.getRootElement();
	     //Get text content
	     String textContent = root.getTextTrim();
	     System.out.println("Text Content: "+textContent);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The trimmed text content along with CDATA section is displayed.

Text Content: <table> - HTML table tag.
Advertisements