今天,学习了DOM4J,发现在eclipse中通过XMLWriter(或者FileWriter)输出XML内容中含有中文,按照默认方式会出现乱码。
package com.edu.scau; import java.io.FileWriter; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class TestFormatWriteout { public static void main(String[] args) throws Exception{ //创建一个document对象,保存要输出的XML对象 Document document = DocumentHelper.createDocument(); //为document添加一个根节点“Root” Element root = document.addElement("Root"); //在根节点root下添加一个子节点"Author" Element author = root .addElement("Author") .addAttribute("name", "Jame") .addText("没有问题"); //创建一个输出格式format OutputFormat format = OutputFormat.createPrettyPrint(); //将document写出到writeFormat.xml文件 XMLWriter writer = new XMLWriter(new FileWriter("writeFormat.xml"), format); writer.write(document); writer.flush(); writer.close(); } }
要使得在“writeFormat.xml”文件中不出现中文乱码,有两个解决方法:
- 在eclipse中设置java源文件的保存格式为“utf-8”:编辑/设置编码(注意当前代码编辑窗口应该是我们要改变编码的java文件)。因为在eclipse中,默认的XML声明中的encoding属性值是“utf-8”,所以,我们的源文件也要保存成“utf-8”格式,才不会出现乱码(关于详细的XML乱码说明请看我的“XML基础语法”)。
- 在输出格式中通过setEncoding("GBK")方法来设定XML声明中的encoding属性值,从而解决中文乱码,因为我们的系统是中文系统(我用的是WIN7旗舰版),Eclipse默认的对源代码的编码是系统的GBK格式,所以通过setEncoding("GBK")来设置XML声明才能生效(保证XML的源文件编码格式与声明的格式一致,才能使解析器正常解析XML文件)。
package com.edu.scau; import java.io.FileWriter; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class TestFormatWriteout { public static void main(String[] args) throws Exception{ //创建一个document对象,保存要输出的XML对象 Document document = DocumentHelper.createDocument(); //为document添加一个根节点“Root” Element root = document.addElement("Root"); //在根节点root下添加一个子节点"Author" Element author = root .addElement("Author") .addAttribute("name", "Jame") .addText("没有问题"); //创建一个输出格式format OutputFormat format = OutputFormat.createPrettyPrint(); //通过setEncoding()方法指定XML编码格式 format.setEncoding("GBK"); //将document写出到writeFormat.xml文件 XMLWriter writer = new XMLWriter(new FileWriter("writeFormat.xml"), format); writer.write(document); writer.flush(); writer.close(); } }