日期:2014-05-20 浏览次数:21031 次
OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK");
------解决方案--------------------
看在你一100分的面子上,给你把代码写全吧。全部代码引用的是 DOM4j文档中的quick start.
package cn.qjwx.xyc.test; import java.io.FileWriter; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class TestJava { /** * @param args */ public static void main(String[] args) { Document document = DocumentHelper.createDocument(); Element root = document.addElement( "root" ); Element author1 = root.addElement( "author" ) .addAttribute( "name", "James" ) .addAttribute( "location", "UK" ) .addText( "James Strachan" ); Element author2 = root.addElement( "author" ) .addAttribute( "name", "Bob" ) .addAttribute( "location", "US" ) .addText( "Bob McWhirter" ); XMLWriter writer; try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK"); writer = new XMLWriter( new FileWriter( "output.xml" ), format ); writer.write( document ); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
------解决方案--------------------
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
加这段代码进去,并且一定要在 writer.write() 之前调用就OK了。
------解决方案--------------------
另外友情提示,输出的流仍然不是 GBK ,相信你能搞定的。。。。。
------解决方案--------------------
上面说的可以。 OutputFormat里设一下.