Java JDOM Document indexOf() Method



The Java JDOM indexOf() method of Document class is used to find the index at which a specific Content object is present inside an XML document. This method returns -1 if the Content object is not found. The Content objects include DocType definition, comments and root element.

Syntax

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

Document.getBaseURI();

Parameters

The Java indexOf() method accepts a single parameter.

content − This 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 content object.

Example 1

Let us see the basic example of using the Java JDOM Document indexOf() method −

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

public class GetContentIndex {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document and adding the root
	     Document doc = new Document();
	     Element element = new Element("root");
	     doc.addContent(element);
	     //Get the index of root
	     int index = doc.indexOf(element);
	     System.out.println("Index of Root Element : "+index);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the index of the given root element.

Index of Root Element : 0

Example 2

The indexOf() method returns -1 when the provided Content object is not found inside the XML document. The following Java program uses Comment as the Content object and illustrates this scenario −

import org.jdom2.Comment;
import org.jdom2.Document;

public class GetContentIndex {
   public static void main(String args[]) {
      try {	
    	 //Creating a new Document
	     Document doc = new Document();
	     Comment comment = new Comment("root");
	     //Get the index of comment
	     int index = doc.indexOf(comment);
	     System.out.println("Index of Comment : "+index);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays -1 as the index.

Index of Comment : -1

Example 3

The indexOf() method returns -1 when we try to find the index of a content object inside an XML document by creating the same content object and passing it as an argument.

Here is the hospital.xml file with one Content object which is the root element with text content.

<hospital>
   Rainbow
</hospital>

In the following program, we have created the same root element as the one present in the hospital.xml file and used the indexOf() method to find the index.

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

public class GetContentIndex {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("hospital.xml");
 		 Document doc = saxBuilder.build(inputFile);		 
 		 //Get the index of element
 		 Element element = new Element("hospital").setText("Rainbow"); 
	     int index = doc.indexOf(element);
	     System.out.println("Index of Element : "+index); 	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the index as -1, that is not found.

Index of Element : -1

Example 4

The getDocument() method is used to create one more instance of the current document. When the Content objects from one document are passed as arguments to indexOf() function and used with second document, this method returns the proper index instead of -1.

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

public class GetContentIndex {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("hospital.xml");
 		 Document doc = saxBuilder.build(inputFile);	
 		 //Get a document copy
 		 Document doc1 = doc.getDocument();
 		 //Get content object from first document
 		 Content con = doc.getContent(0); 
 		 //Check the index in second document
	     int index = doc1.indexOf(con);
	     System.out.println("Index of Content object in doc1 : "+index); 	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The index of the content object is displayed on the output screen.

Index of Content object in doc1 : 0
Advertisements