可使用OutputKeys对输出进行格式化
        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 );