1、写入文件
1.1 简易的写入
初始化一个XMLOutputter,重点在Format,我们通过它来设置一些需要的东西,比如编码。
org.jdom.output.XMLOutputter(org.jdom.output.Format format)
获取Format
三种方法
1、Format.getPrettyFormat() 2、Format.getRawFormat() 3、Format.getCompactFormat()
不言而喻,默认第二种,如果使用第二种,那么当你用文本编辑器看的时候及时一团糟。
使用第一种则会加上换行。这里要注意,默认的换行DDS-style是CR-LF(/r/n),而Unix下
是 (/n)所以,如果我们不希望在Unix的XML生成以后出现 ^M着这种符号,那么就是用
format.setLineSeparator("/n");
设置Format,主要是编码方式,默认UTF-8,如果你的是Unix系统或者Linux系统,使用上面
的方法设置format.setLineSeparator("/n")
初始化XMLOutputter后,我们就可以通过一个FileOutputStream写文件了。
Document doc = ... XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); FileOutputStream fos = new FileOutputStream(new File("test.xml")); out.output(doc, fos); fos.clos();