Java JDOM Element getChildren() Method



The Java JDOM getChildren() method of Element class retrieves the child elements one level deep inside an XML Element. Using this method, only those elements with a specified local name and within a specified namespace can be obtained.

The getChildren() method returns a live list of Element objects and hence making any changes to the elements affect the actual content list of the element. This method returns an empty list if the current element has no child elements.

Syntax

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

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

Parameters

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

  • cname − represents the local name of an XML element.
  • ns − Namespace that is attached to the element that needs to be retrieved.

Return Value

The Java getChildren() method returns the list of child elements inside the XML element.

Example 1

We need to parse the following vehicle.xml file −

<vehicle>
	<twoWheeler>Bike
       <bike>Royal Enfield</bike>
       <bike>Honda Activa</bike>
       <bike>Bajaj Pulsar</bike>
	</twoWheeler>
	<twoWheeler>Scooty</twoWheeler>
	<fourWheeler>Car</fourWheeler>
	<fourWheeler>Jeep</fourWheeler>	
</vehicle>

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

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

public class GetChildren {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("vehicle.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get child elements
    	 List<Element> list = root.getChildren();	     
	     //Print child elements
    	 System.out.println("List of vehicles : ");
    	 for(Element child : list)
    	    System.out.println(child.getText());	       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}
List of vehicles : 
Bike	
Scooty
Car
Jeep

Example 2

The getChildren() method takes local name of a child element as an argument and returns all the child elements with that name one level deep inside the current element.

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

public class GetChildren {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("vehicle.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get child elements
    	 List<Element> list = root.getChildren("fourWheeler");	     
	     //Print child elements
    	 System.out.println("List of four wheelers : ");
    	 for(Element child : list)   		
    	    System.out.println(child.getText());
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

All the available four wheelers are displayed.

List of four wheelers : 
Car
Jeep

Example 3

The getChildren() method can be used to retrieve the child elements inside a child element by calling the same function on child element.

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

public class GetChildren {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("vehicle.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get child elements
    	 List<Element> twoWheelerList = root.getChildren("twoWheeler");
    	 List<Element> bikeList = twoWheelerList.get(0).getChildren();
	     //Print Bike elements
    	 System.out.println("List of available bikes : ");
    	 for(Element bike : bikeList)   		
    	    System.out.println(bike.getText());
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The list of bikes inside the element two wheelers is displayed.

List of available bikes : 
Royal Enfield
Honda Activa
Bajaj Pulsar
Advertisements