Java JDOM Document indexOf() 方法



Java JDOM 的 indexOf() 方法用于查找特定 Content 对象在 XML 文档中的索引。如果找不到 Content 对象,则此方法返回 -1。Content 对象包括 DocType 定义、注释和根元素。

语法

以下是 Java JDOM Document indexOf() 方法的语法:

Document.getBaseURI();

参数

Java indexOf() 方法接受一个参数。

content − 表示需要查找其索引的 Content 对象。

返回值

Java indexOf() 方法返回一个整数值,表示 Content 对象的索引。

示例 1

让我们来看一下使用 Java JDOM Document indexOf() 方法的基本示例:

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();
      }
   }
}

输出窗口显示给定根元素的索引。

Index of Root Element : 0

示例 2

当提供的 Content 对象在 XML 文档中找不到时,indexOf() 方法返回 -1。下面的 Java 程序使用 Comment 作为 Content 对象,并说明了这种情况:

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();
      }
   }
}

输出窗口显示索引为 -1。

Index of Comment : -1

示例 3

当我们尝试通过创建相同的 Content 对象并将其作为参数传递来查找 XML 文档中 Content 对象的索引时,indexOf() 方法返回 -1。

这是包含一个 Content 对象(根元素及其文本内容)的 hospital.xml 文件。

<hospital>
   Rainbow
</hospital>

在下面的程序中,我们创建了与 hospital.xml 文件中存在的根元素相同的根元素,并使用 indexOf() 方法查找索引。

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();
      }
   }
}

输出窗口显示索引为 -1,表示未找到。

Index of Element : -1

示例 4

getDocument() 方法用于创建当前文档的另一个实例。当从一个文档中的 Content 对象作为参数传递给 indexOf() 函数并与第二个文档一起使用时,此方法返回正确的索引而不是 -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();
      }
   }
}

Content 对象的索引显示在输出屏幕上。

Index of Content object in doc1 : 0
广告
© . All rights reserved.