DOM4J 写入 XML 文件

项目依赖:

<dependency>
	<groupId>dom4j</groupId>
	<artifactId>dom4j</artifactId>
	<version>1.6.1</version>
</dependency>

生成 sitemap.xml 文件:

// xmlString 是符合 XML 格式的字符串;
// 根据 XML 字符串生成 org.dom4j.Document 对象;
Document document = DocumentHelper.parseText(xmlString);
// 指定 document 字符编码;
document.setXMLEncoding("UTF-8");
String path = "E:\\xml\\";
String fileName = "sitemap.xml";
String realPath = path + fileName;
File file = new File(realPath);
// 生成相应路径;
new File(path).mkdirs();
OutputStream out = new FileOutputStream(file);
Writer fileWriter = new OutputStreamWriter(out, "UTF-8");
XMLWriter xmlWriter = new XMLWriter(fileWriter);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();


你可能感兴趣的:(java,xml,dom)