- 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();
- }
- }
解决java写xml中文乱码问题
最新推荐文章于 2024-04-11 15:00:23 发布