jdom出来xml文件比较方便,尤其是在读写文件的时候,但出xml文件的时候,要format一下,这样看起来比较方便,因项目需要和别的公司做接口,传输就采用xml文件,起初写xml文件时候没格式化,不过输出实在不好看.今天晚上就写了个程序格式化,发上来大家一起学习.

public void writeFormatXML(Document doc, String filename)
{
String indent = " "; // 缩进符号
boolean newLines = true; // 是否产生新行
XMLOutputter outp = new XMLOutputter(indent, newLines, "gb2312"); // 构造新的输出流

try
{
outp.output(doc, new FileOutputStream(filename));

} catch (FileNotFoundException e)
{
e.printStackTrace();

} catch (IOException e)
{
e.printStackTrace();
}
}