Java JDOM Element addContent() 方法



Java JDOM 的 addContent() 方法是 Element 类的成员方法,用于在 XML 元素内部添加内容对象。这些内容对象包括文本、元素和注释。仅允许合法的內容对象。例如,处理指令、DocType声明等内容对象不允许在 XML 元素内部使用。

如果我们尝试添加的内容对象的父元素已存在,或者它是不合法的內容对象,则 addContent() 方法会抛出 illegalAddException 异常。当添加内容时提供错误的索引时,也会抛出此异常。

语法

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

Element.addContent(str);
Element.addContent(child);
Element.addContent(list);
Element.addContent(index, child);
Element.addContent(index, list);

参数

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

  • str - 要添加到元素的文本内容。
  • child - 需要添加的子内容对象。
  • list - 要添加的内容对象的列表。
  • index - 需要添加内容的索引。

返回值

Java addContent() 方法在添加内容后返回 Element 对象。

示例 1

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

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

public class AddChildElement {
   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");
	     //Add element
	     root.addContent(child);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

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

<?xml version="1.0" encoding="UTF-8"?>
<root>
  I'm root.
  <child />
</root>

示例 2

addContent() 方法将字符串作为参数,并将其作为文本内容添加到 Element 对象。如果文本内容已存在,则会将提供的字符串附加到现有的文本内容。

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

public class AddTextContent {
   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 and add text content
	     String textContent = "Extra text content added.";
	     root.addContent(textContent);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

添加文本内容后,更新的文档将显示。

<?xml version="1.0" encoding="UTF-8"?>
<root>I'm root. Extra text content added.</root>

示例 3

默认情况下,addContent() 方法将内容列表或内容对象插入到已存在的内容列表的末尾。要将内容插入指定位置,必须将索引作为第一个参数传递。

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 AddComment {
   public static void main(String args[]) {
      try {	
    	 //Reading the document and get root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Create comment
	     Comment comment = new Comment("Here is the info of child...");
	     //Add comment at index 0
	     root.addContent(0,comment);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

添加注释后,更新的文档将显示。

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <!--Here is the info of child...-->
  <child />
</root>

示例 4

addContent() 方法也可用于将不同内容对象的列表添加到 XML 元素。

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

public class AddContentList {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document and add root
	     Document doc = new Document();
	     Element root = new Element("root");
	     doc.setRootElement(root);
	     //Create child objects
	     Comment comment = new Comment("Here is the info of children...");
	     Element child1 = new Element("child1");
	     Element child2 = new Element("child2");
	     //create list and add content objects
	     List<Content> list = new ArrayList<>();	    
	     list.add(comment);
	     list.add(child1);
	     list.add(child2);
	     //Add content 
	     root.addContent(list);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

添加内容列表后,更新的文档将显示。

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <!--Here is the info of children...-->
  <child1 />
  <child2 />
</root>
广告

© . All rights reserved.