public static void output(Node node, String filename) {  
        TransformerFactory transFactory = TransformerFactory.newInstance();  
        try {  
          Transformer transformer = transFactory.newTransformer();  
          // 设置各种输出属性  
          transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); 
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");  
          DOMSource source = new DOMSource(); 
          // 将待转换输出节点赋值给DOM源模型的持有者(holder)  
          source.setNode(node);  
          StreamResult result = new StreamResult();  
          if (filename == null) {  
            // 设置标准输出流为transformer的底层输出目标  
            result.setOutputStream(System.out);  
          } else {  
            result.setOutputStream(new FileOutputStream(filename));  
          }  
          // 执行转换从源模型到控制台输出流  
          transformer.transform(source, result);  
        } catch (TransformerConfigurationException e) {  
          e.printStackTrace();  
        } catch (TransformerException e) {  
          e.printStackTrace();  
        } catch (FileNotFoundException e) {  
          e.printStackTrace();  
        }  
      }