java xmlwriter 生成 xml 文件多了一行
因为newLineAfterDeclaration 默认为true ,会在xml声明后生成一行空行
所以需要指定
format.setNewLineAfterDeclaration(false);
总的代码
try(FileOutputStream fileOutputStream=new FileOutputStream(fileName)){ OutputFormat format =OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); format.setNewLineAfterDeclaration(false); XMLWriter writer = new XMLWriter(fileOutputStream, format); writer.setScapeText(false); writer.writer(document); writer.flush(); writer.close(); } cat(IOException e){ log.error("",e); }