Java JDOM Element removeContent() 方法



Java JDOM 的 removeContent() 方法是 Element 类的方法,用于移除 XML 元素内的内容对象。这些内容对象包括注释、文本和子元素等。使用此方法,可以移除指定索引处的任何内容对象。也可以使用此方法移除指定类型的内容对象。

语法

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

Element.removeContent();
Element.removeContent(child);
Element.removeContent(filter);
Element.removeContent(index);

参数

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

  • child − 表示需要移除的内容对象。
  • filter − 表示特定内容类型的过滤器对象。
  • index − 表示需要移除的内容对象的索引。

返回值

Java removeContent() 方法返回已移除的内容对象。

示例 1

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

<!-- Root information is available here -->
<root> I'm the root
<!-- This root has three children -->
   <child>First child</child>
   <child>Second child</child>	
</root>

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

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 RemoveContent {
   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();
	     //Remove content
    	 List<Content> conList = root.removeContent();
    	 System.out.println(conList); 
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示已移除的内容对象列表。

[[Text:  I'm the root
], [Comment: <!-- This root has three children -->], [Text: 
   ], [Element: <child/>], [Text: 
   ], [Element: <child/>], [Text: 	
]]

示例 2

removeContent() 方法可用于通过将索引作为参数传递来移除指定索引处的内容对象。

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

public class RemoveContent {
   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();
	     //Remove content
    	 Content con = root.removeContent(0);
    	 System.out.println(con); 
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示已移除的文本内容对象。

[Text:  I'm the root
]

示例 3

removeContent() 方法可用于通过将过滤器对象作为参数传递来移除特定内容对象。

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 RemoveContent {
   public static void main(String args[]) {
      try {
         //Reading the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
    	 //Remove only elements
    	 Filter<Element> element_filter = Filters.element();
    	 List<Element> elementList = root.removeContent(element_filter);
    	 for(Content con : elementList) {
    		System.out.println(con);
    	 }	  
      } catch(Exception e) {
    	 e.printStackTrace();
      }
   }
}

输出窗口显示已移除的子元素。

[Element: <child/>]
[Element: <child/>]
广告
© . All rights reserved.