Java JDOM Element getContent() 方法



Java JDOM 的 getContent() 方法是 Element 类的,用于获取 XML 元素作用域内的内容对象。内容对象包括 Element、Text、Comment、ProcessingInstruction、CDATA 和 EntityRef。

getContent() 方法也可用于仅检索特定类型的内容,例如仅元素、仅注释等。此方法可用于获取指定索引处的内容对象。它返回一个动态列表,因此对内容对象所做的任何修改都会影响原始内容列表。

语法

以下是 Java JDOM Element getContent() 方法的语法:

Element.getContent();
Element.getContent(index);
Element.getContent(filter);

参数

Java getContent() 方法是一个多态方法,它接受以下参数:

  • index - 表示内容对象的索引。
  • filter - 表示用于过滤元素、注释等的过滤器对象。

返回值

Java getContent() 方法返回内容对象的列表。

示例 1

我们需要解析以下 library.xml 文件:

<library> Readers choice
	<!--Book information is available here -->
   <book>War and peace</book>
   <book>Random thoughts</book>
</library>

以下是使用 Java JDOM Element getContent() 方法的基本示例:

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

public class GetContent {
   public static void main(String args[]) {
      try {
         //Reading the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("library.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get content
    	 List<Content> contentList = root.getContent();
    	 for(Content con : contentList) {
    		System.out.println(con);
    	 }	  
      } catch(Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示根元素的内容对象列表。

[Text:  Readers choice
	]
[Comment: <!--Book information is available here -->]
[Text: 
   ]
[Element: <book/>]
[Text: 
   ]
[Element: <book/>]
[Text: 
]

示例 2

getContent() 方法通过将索引作为参数传递给它来检索指定索引处的内容对象。

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

public class GetContent {
   public static void main(String args[]) {
      try {
         //Reading the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("library.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get content at index 1
    	 Content content = root.getContent(1);
    	 System.out.println(content);	  
      } catch(Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示索引 1 处的内容对象。

[Comment: <!--Book information is available here -->]

示例 3

Filters.element() 函数仅过滤 Element 类型的内容对象。此 Element 过滤器作为参数传递到 getContent() 方法中,以仅获取元素对象。

import java.io.File;
import java.util.List;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;

public class GetContent {
   public static void main(String args[]) {
      try {
         //Reading the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("library.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Get only elements
    	 Filter<Element> element_filter = Filters.element();
    	 List<Element> elementList = root.getContent(element_filter);
    	 for(Content con : elementList) {
    		System.out.println(con);
    	 }	  
      } catch(Exception e) {
    	 e.printStackTrace();
      }
   }
}

仅显示当前元素内的元素。

[Element: <book/>]
[Element: <book/>]
广告

© . All rights reserved.