您的位置:首页 > 编程语言 > Java开发

解决java写xml中文乱码问题

2017-02-24 17:12 387 查看
[java] view
plain copy

print?

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: