DOM4J 写入 XML 文件

来源:互联网 发布:网络大电影合作流程 编辑:程序博客网 时间:2024/05/16 12:43

项目依赖:

<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();


0 0
原创粉丝点击