Java JDOM Element setContent() 方法



Java JDOM 的 setContent() 方法是 Element 类的,用于设置 XML 元素内部的内容。使用此方法,我们可以通过替换已存在的内容来添加新内容。我们可以使用此方法添加单个对象或内容对象的列表。

当传递空内容列表作为参数时,setContent() 方法会清除 XML 元素的全部内容。如果发生异常,内容列表将不会添加到元素中,并且新内容列表保持不变。

语法

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

Element.setContent(child);
Element.setContent(index, child);
Element.setContent(contentList);
Element.setContent(index, contentList);

参数

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

  • child - 代表一个子 Content 对象。
  • index - 代表需要设置内容对象的索引。
  • contentList - 代表内容对象的列表。

返回值

Java setContent() 方法返回设置内容的元素。

示例 1

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

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class setChildElement {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Create a child element
	     Element child = new Element("child");
	     //Set child element
	     root.setContent(child);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

添加子元素后,更新的 XML 文档将显示。

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <child />
</root>

示例 2

使用 setContent() 方法,我们可以一次设置内容对象的列表,方法是将内容列表作为参数传递给它。

import org.jdom2.Document;
import org.jdom2.Element;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Content;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class setChildElement {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Create a child elements
	     Element child1 = new Element("child1");
	     Element child2 = new Element("child2");
	     List<Content> contentList = new ArrayList<Content>();
	     contentList.add(child1);
	     contentList.add(child2);
	     //Set content list to root
	     root.setContent(contentList);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

添加子元素列表后,更新的 XML 文档将显示。

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <child1 />
  <child2 />
</root>

示例 3

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

<library> Readers choice
   <book>War and peace</book>
   <book>Random thoughts</book>
</library>

setContent() 方法用提供的对象替换指定索引处的内容对象。

import java.io.File;

import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class ReplaceContent {
   public static void main(String args[]) {
      try {	
    	 //Reading the document and get root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("src/setContent/sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Create comment
	     Comment comment = new Comment("Book information is available here");
	     //Add comment at index 0
	     root.setContent(1,comment);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示替换内容对象后的 XML 文档。

<?xml version="1.0" encoding="UTF-8"?>
<library>
  Readers choice
  <!--Book information is available here-->
  <book>Random thoughts</book>
</library>
广告

© . All rights reserved.