针对上篇文章中遇到的xml文件保存问题,经过查找资料,发现还可以采用如下方式:
/**
* 功能:保存xml文件
* @param doc
* @param file 文件路径
*/
private static void writeXML(Document doc, String file) {
try {
CommonOS cos=new CommonOS();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty("indent", "yes");
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://localhost:8083/jstab/xml/AbsXml.dtd");//保留dtd
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(cos.getWebRoot()+"/xml/"+file)));
} catch (Exception e) {
e.printStackTrace();
}
}
需要说明的是
(1)这儿需要下载【最新的】Xerces-J-bin.2.9.1jar包,从apache网站下下载即可。