本文共 920 字,大约阅读时间需要 3 分钟。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class CreateXML {
public void create(){
Element root=new Element("PEOPLE");
Document doc=new Document(root);
Element person=new Element("PERSON");
root.addContent(person);
Attribute attribute=new Attribute("title","这是一个测试的文字");
person.setAttribute(attribute);
XMLOutputter output=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8"));
try {
//FileWriter writer=new FileWriter("src/people.xml");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("src/people.xml"), "UTF-8");
output.output(doc, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CreateXML().create();
}
}
转载地址:http://ujima.baihongyu.com/