/**
 * 这个方法使用XMLOutputter将JDom Document对象输出到文件
 * @param doc 将要输出的JDom Document对象
 * @param filePath 将要输出到的磁盘路径
 * @param newline 是否换行
 * @param encoding 编码方式
 */
public static void OutputToFile(
    Document myDocument,
    String filePath,
    boolean newline,
    String encoding) {
    try {
        XMLOutputter outputter = new XMLOutputter("  ", newline, encoding);
        FileWriter writer = new FileWriter(filePath);
        outputter.output(doc, writer);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

解决方案 »

  1.   

    XMLOutputter 有这样的构造函数么?
    XMLOutputter outputter = new XMLOutputter("  ", newline, encoding);
    *******************************************************************
    下面是api文档中给出的三个构造函数的说明,没有大哥你说的那样的构造啊
    Constructor Summary 
    XMLOutputter() 
              This will create an XMLOutputter with the default Format matching Format.getRawFormat(). 
    XMLOutputter(Format format) 
              This will create an XMLOutputter with the specified format characteristics. 
    XMLOutputter(XMLOutputter that) 
              This will create an XMLOutputter with all the options as set in the given XMLOutputter. 
      

  2.   

    public XMLOutputter(String indent, boolean newlines, String encoding) {
            userFormat.setIndent(indent);
            if (newlines == false) userFormat.setIndent(null);
            userFormat.setEncoding(encoding);
        }我用的是jdom b10
      

  3.   

    我知道了,我用的是jdom1.0,api已经改变了,必须要先构造一个Format类的一个对象,然后再用XMLOutputter(Format format)来初始化,就可以生成我要的东西了:
    Format format = Format.getPrettyFormat();
    format.setEncoding("GBK");
    XMLOutputter outp = new XMLOutputter(format);
    outp.output(xmlDocument, new FileOutputStream("c:\\subject_data3_gbk.xml")); //输出XML文档
    其中Format类的构在方法是private的必须用下面三个方法实例化
    getRawFormat() (no whitespace changes), 
    getPrettyFormat() (whitespace beautification), 
    getCompactFormat() (whitespace normalization). 
    并且实例化的Format再输出时都不一样。
      

  4.   

    虽然已经结贴,支持一下逆风兄。b10不提倡那种初始化了。文档中已经说明了是Deprecated的