Java 怎么在文件中输出和转换(Transform)XML数据

2019年7月29日21:05:02Java 怎么在文件中输出和转换(Transform)XML数据已关闭评论 512 728字阅读2分25秒

1.简介

Java 怎么在文件中输出和转换(Transform)XML数据?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import org.w3c.dom.Document;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class Main {
    
    public static void buildXmlFile(Document doc, String path) {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = tfactory.newTransformer();
            DOMSource source = new DOMSource(doc);

            StreamResult result = new StreamResult(new File(path));
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("encoding", "UTF-8");
            transformer.transform(source, result);

        } catch (TransformerConfigurationException e) {
            
        } catch (TransformerException e) {

        }
    }
}

 
编程十万个怎么办