原因 我认为主要是因为中文.而且有时候能打开有时候打不开.加上这个 好些鸟.我这个 也是搬的
Java code
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了。