
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
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.