今天,学习了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”文件中不出现中文乱码,有两个解决方法:
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(); } }