简介
根据xml模板动态填充xml;在网上找了很多跟我需要的都不一样,自己在这写一下,源码地址拿来改一下模板就可以直接使用
代码
核心依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
xml模板
<?xml version="1.0" encoding="utf-8"?>
<INFO>这就是一个简单的模板</INFO>
<Profile>
<ID>${id!''}</ID>
<Name>${name!''}</Name>
<#list demoList as list>
<Demo>
<Subject>${list.subject}</Subject>
<Score>${list.score}</Score>
</Demo>
</#list>
</Profile>
代码
package com.fanqiechaodan.generator.controller;
import com.fanqiechaodan.generator.model.Demo;
import com.fanqiechaodan.generator.model.XMLModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @Classname XMLController
* @Description
* @Date 2022/6/19 19:10
* @Author: fanqiechaodan
*/
@RestController
@RequestMapping(value = "/xml")
@Slf4j
public class XMLController {
@GetMapping(value = "/generator")
public void generator() {
List<Demo> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Demo demo = new Demo();
demo.setScore(60 + i);
demo.setSubject("subject" + i);
list.add(demo);
}
XMLModel xmlModel = new XMLModel();
xmlModel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
xmlModel.setName("张三");
xmlModel.setDemoList(list);
xml2XmlDoc(xmlModel, "src\\main\\resources\\template\\template.xml", "src\\main\\resources\\template\\newTemplate.xml");
}
/**
* 将xml模板转换为newxml
*
* @param model 需要填充到模板的数据
* @param templetFilePath 模板文件路径
* @param targetFilePath 目标文件保存路径
*/
public static void xml2XmlDoc(XMLModel model, String templetFilePath, String targetFilePath) {
Writer out = null;
try {
// 将模板文件路径拆分为文件夹路径和文件名称
String tempLetDir = templetFilePath.substring(0, templetFilePath.lastIndexOf("\\"));
// 注意:templetFilePath.lastIndexOf("/")中,有的文件分隔符为:\ 要注意文件路径的分隔符
String templetName = templetFilePath.substring(templetFilePath.lastIndexOf("\\") + 1);
// 将目标文件保存路径拆分为文件夹路径和文件名称
String targetDir = targetFilePath.substring(0, targetFilePath.lastIndexOf("\\"));
String targetName = targetFilePath.substring(targetFilePath.lastIndexOf("\\") + 1);
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setDefaultEncoding(String.valueOf(StandardCharsets.UTF_8));
// 如果目标文件目录不存在创建
File file = new File(targetDir);
if (!file.exists()) {
file.mkdirs();
}
//加载模板数据(从文件路径中获取文件)
configuration.setDirectoryForTemplateLoading(new File(tempLetDir));
//获取模板实例
Template template = configuration.getTemplate(templetName);
File outFile = new File(targetDir + File.separator + targetName);
//模板和数据模型合并生成文件
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8));
//生成文件
template.process(model, out);
out.flush();
out.close();
} catch (Exception e) {
log.error("write xml failed:", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error("close out failed:", e);
}
}
}
}
}
测试