试试
        DOMSource doms = new DOMSource( document );
        File file = new File( name );
        StreamResult result = new StreamResult( file );
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        Properties properties = transformer.getOutputProperties();
        properties.setProperty( OutputKeys.ENCODING, "GB2312" );
        properties.setProperty( OutputKeys.METHOD, "xml" );
        properties.setProperty( OutputKeys.INDENT, "yes" );
        transformer.setOutputProperties( properties );
        transformer.transform( doms, result );

解决方案 »

  1.   

    You can try replacing DOMSource with StreamSource .
      

  2.   

    to wangwenyou(王文友) :
         Thank you, what's purpose of setting the properties?
      

  3.   

    可他是Save Document,不可能构造StreamSource啊
      

  4.   

    to wangwenyou(王文友) :
         Thank you, what's purpose of setting the properties?
      

  5.   

    to wangwenyou(王文友) :
         Thank you, what's purpose of setting the properties?
      

  6.   

    客气!
    主要是为了指定输出为XML,而且输出文件的编码为GB2312,还有输出格式
      

  7.   

    even i set properties, it still can not, I am working in an enviroment which use xalan and crismon for dealing with xml document. Since I am using English OS,properties.setProperty( OutputKeys.ENCODING, "GB2312" );would not be neccssary, right?
      

  8.   

    关于Xml的保存,有几种办法,你可以看看
    http://www.csdn.net/Expert/TopicView1.asp?id=722635
      

  9.   

    对,不需要了
    不过parser我建议你还是由Crimson转到Xerces,Crimson太慢
      

  10.   

    对,不需要了
    不过parser我建议你还是由Crimson转到Xerces,Crimson太慢
      

  11.   

    i am developing an application under the portal environment, since they are using that, it seems not possible for me to replace it, otherwise i suspend there will be unexpected error to the system. the problem is, as long as i developed appliction following the jaxp refence implementation, i should be able to replace it with others. like JDBC driver. wondering why I this error occurs.
      

  12.   

    唉,那就只有继续Crimson了:)
    不过也没关系,代码都一样
    你的错误看不出所以然来,一般说来应该是Classpath的问题,几个互相冲突的Xml相关jar文件,先调用了旧版本的。仔细检查你的ClassPath和JAVA_HOME/Lib/ext,注意先后顺序。
      

  13.   

    I guess you are right, 
    as i am running the application under another environment, it included some xml parsers in the library file. Looing into the classpath file won't help. If, I replace the old crimson with xerces, do you expect any problem?
      

  14.   

    不一定要用Xerces替换Crimson,你只要看看几个包含org.apache.xml.*的jar就成,注意一定要把新版本的放前面,但如果你把jar放到某个引用的目录,那可就麻烦了。并且我想改用Xerces对你们已有的代码影响也不会太大。