求助:怎样把一个已生成的org.w3c.dom.document写入XML文件中??

我用xml4j.jar中的org.w3c.dom.document生成了一个document在内存中,有比较通用的方法把一次性写入到新的XML文件中么(或者说是跟据这个document生成一个XML文件)?如果有好的解决办法,给高分,不够我开贴再加,在线等待
---------------------------------------------------------------

TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer transformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312

transformer.setOutputProperty("encoding","gb2312");
DOMSource source= new DOMSource(doc);

StreamResult result = new StreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationException e){
e.printStackTrace();
System.out.println(e.getMessage());
return 3;//写文件错误

Published At
Categories with Web编程
comments powered by Disqus