Java JDOM Element indexOf() Method



The Java JDOM indexOf() method of Element class is used to get the index of the child content inside an XML Element. Content objects can be elements, comments etc. The indexing for the child content objects start with 0. This method returns -1 if there is no such content object inside the element.

Syntax

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

Element.indexOf(child);

Parameters

The Java indexOf() method accepts a single parameter.

child − represents a content object whose index needs to be found.

Return Value

The Java indexOf() method returns an integer value that represents the index of the child content.

Example 1

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

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

public class IndexOfChild {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add elements
	     Document doc = new Document();
	     Element root = new Element("root");
	     doc.setRootElement(root);
	     Element child = new Element("child");
	     root.addContent(child);
	     //Get the index of child
	     int index = root.indexOf(child);
	     System.out.println("Index of child : " + index);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

Index of the child element is displayed.

Index of child : 0

Example 2

The indexOf() method returns -1 if there is no such child content inside the scope of the XML Element.

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

public class IndexOfChild {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add elements
	     Document doc = new Document();
	     Element root = new Element("root");
	     doc.setRootElement(root);
	     Element child = new Element("child");
	     //Get the index of child
	     int index = root.indexOf(child);
	     System.out.println("Index of child : " + index);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The index of the element is displayed as -1.

Index of child : -1

Example 3

Here is the sample.xml file we need to parse −

<root>
	<child1>First child</child1>
	<child2>Second child</child2>
	<child3>Third child</child3>
</root>

The indexOf() method returns the indices by counting the whitespace as a content object after the end of each element.

import java.io.File;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class IndexOfChild {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 List<Element> list = root.getChildren();	     
	     //Get indices of children
    	 for(int i=1;i<=list.size();i++) {
    	    int index = root.indexOf(list.get(i-1));
    	    System.out.println("Index of child"+i+" : "+ index);	 
    	 }       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The indices of the child elements are displayed.

Index of child1 : 1
Index of child2 : 3
Index of child3 : 5
Advertisements