private static void createXml(String dest) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element elem = doc.createElement("xie");
doc.appendChild(elem);
//写出XML文档
writerXml(doc, dest);
}
private static void writerXml(Document doc,String destFile) throws Exception{
/*
* 写出到xml文件中的步骤
* 1、获取格式化工厂
* 2、获取格式器
* 3、设置编码
* 4、转换资源
*/
TransformerFactory fac = TransformerFactory.newInstance();
Transformer trans = fac.newTransformer();
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
Source xmlSource = new DOMSource(doc);
File file = new File(destFile);
Result outputTarget = new StreamResult(file);
trans.transform(xmlSource, outputTarget);
}
java动态创建xml文件
最新推荐文章于 2019-08-09 21:01:55 发布