/**
* 保存节点到文件
* @param root 需要保存的节点
* @param fileName 保存文件的名称
*/
public static void saveXml(Node root,String fileName){
try {
//1 获得转换工厂
TransformerFactory transformerFactory = TransformerFactory.newInstance();
//2 获得转换器
Transformer transformer = transformerFactory.newTransformer();
//3.1 将节点封装成转换器需要的源 ,此处使用的是Source接口与dom有关的实现类DOMSource
Source xmlSource = new DOMSource(root);http://www.huiyi8.com/gundongdaima/ 滚动代码
//3.2 将保存文件的位置封装转换器需要的结果对象,此处使用的是Result与文件流有关的实现类SteamResult
Result outputTarget = new StreamResult(new File(fileName));
//4 保存数据,及将指定的节点保存到指定的文件中
transformer.transform(xmlSource, outputTarget);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);
* 保存节点到文件
* @param root 需要保存的节点
* @param fileName 保存文件的名称
*/
public static void saveXml(Node root,String fileName){
try {
//1 获得转换工厂
TransformerFactory transformerFactory = TransformerFactory.newInstance();
//2 获得转换器
Transformer transformer = transformerFactory.newTransformer();
//3.1 将节点封装成转换器需要的源 ,此处使用的是Source接口与dom有关的实现类DOMSource
Source xmlSource = new DOMSource(root);http://www.huiyi8.com/gundongdaima/ 滚动代码
//3.2 将保存文件的位置封装转换器需要的结果对象,此处使用的是Result与文件流有关的实现类SteamResult
Result outputTarget = new StreamResult(new File(fileName));
//4 保存数据,及将指定的节点保存到指定的文件中
transformer.transform(xmlSource, outputTarget);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);