在JAVA中使用DOM將xml文件修改後,想將修改後的結果保存到xml文件中,這時需要寫一個函數來完。
public void writeToXml(Document doc,String rptdesign){
try{
OutputStream fileoutputStream = new FileOutputStream(rptdesign);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fileoutputStream);
transformer.transform(source, result);
}
catch(Exception e){
toPrint("Can't write to file: "+ rptdesign);
return;
}
}
其中,doc是已修改好的內存中的document信息,rptdesign是要保存到的文件名其路徑。