Java JDOM Element getTextNormalize() Method



The Java JDOM getTextNormalize() method of Element class is used to get the normalized text content of an XML element. It removes the whitespaces at the beginning and end of the text content and also retains a single whitespace and removes the extra whitespaces in the middle of the text content. It also returns the CDATA section if exists.

Syntax

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

Element.getTextNormalize();

Parameters

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

Return Value

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

Example 1

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

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

public class GetNormalizedText {
   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.getTextNormalize();
	     System.out.println("Text Content: "+textContent);
	     
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The normalized text content is displayed on the output screen.

Text Content: War and Peace

Example 2

The Java getTextNormalize() 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 GetNormalizedText {
   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.getTextNormalize();
	     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 getTextNormalize() 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 GetNormalizedText {
   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.getTextNormalize();
	     System.out.println("Text Content: "+textContent);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The normalized text content along with CDATA section is displayed.

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