Java JDOM Element getChildText() Method



The Java JDOM getChildText() method of Element class retrieves the text content of a child element. This method returns null if there is no such child element with the specified name. This method can also be used to get the text content of a child element within a specified namespace.

Syntax

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

Element.getChildText(cname);
Element.getChildText(cname, ns);

Parameters

The Java getChildText() method is a polymorphic method and it accepts the following parameters −

  • cname − represents the local name of the child element.
  • ns − Namespace that is attached to the child element whose text content needs to be retrieved.

Return Value

The Java getChildText() method returns the text content of the child in the form of a string.

Example 1

We need to parse the following vehicles.xml file −

<vehicle>
   <twoWheeler>Bike</twoWheeler>
   <twoWheeler xmlns ="https://namespace/twoWheeler">Scooty</twoWheeler>
   <fourWheeler>Car</fourWheeler>
</vehicle>

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

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

public class GetChildText {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("vehicles.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get child element text
    	 String text = root.getChildText("twoWheeler");     
    	 System.out.println("Text Content of the element with name, 'twoWheeler': "+text);	       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

Text content of the child element is is displayed.

Text Content of the element with name, 'twoWheeler': Bike

Example 2

The getChildText() method returns null if there is no such child element with the specified name.

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

public class GetChildText {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("vehicles.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get child element text
    	 String text = root.getChildText("threeWheeler");     
    	 System.out.println("Text Content of the element with name, 'threeWheeler': "+text);	       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

Text content of the child element is is displayed as null.

Text Content of the element with name, 'threeWheeler': null

Example 3

The getChildText() method can be used to obtain the text content of the child element within a specified namespace as follows −

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

public class GetChildText {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("vehicles.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get child element text
    	 Namespace ns = Namespace.getNamespace("https://namespace/twoWheeler");
    	 String text = root.getChildText("twoWheeler",ns);     
    	 System.out.println("Text Content of the element with name, 'twoWheeler' within given namespace : "+text);	       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

Text content of the child element within a namespace is displayed

Text Content of the element with name, 'twoWheeler' within given namespace : Scooty
Advertisements