freemarker生成xml文件

记录一次用freeMarker生成xml文件
import freemarker.template.Configuration;
import freemarker.template.Template;
/*
*uiName  要生成xml文件的名称
*xmlName 模板ftl的名称
*/
public static void createXml(String uiName,String xmlName) {
Writer w  = null;
    	try {
    	//获取xmlTemplate文件夹的当前路径
    		URL url = Thread.currentThread().getContextClassLoader().getResource("xmlTemplate");
			String path = url.getPath();
    		Configuration configuration = new Configuration();
			configuration.setDefaultEncoding("utf-8");
			configuration.setDirectoryForTemplateLoading(new File(path));
			//解决写入到xml文件出现乱码问题
			Template template = configuration.getTemplate(xmlName+".ftl","utf-8");
			 Map<String, Object> responseMap = new HashMap<String, Object>();
			        responseMap.put("id", "1");
			        responseMap.put("name", "编号");
			        responseMap.put("value", "007");
			File file = new File("D:\\"+uiName+".xml");
			FileOutputStream f = new FileOutputStream(file);
			 w = new OutputStreamWriter(f,"utf-8");
			template.process(responseMap, w);
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		w.close();
	}
	}
ftl模板
<?xml version="1.0" encoding="UTF-8"?>
<properties>
	<property>
		<id>${id!}</id>
		<name>${name!}</name>
		<value>${value!}</value>
	</property>
</properties>	

如有不对的地方望指出,感谢!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中使用 Freemarker 生成 XML 文件,需要先添加依赖。 Maven 依赖: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> ``` Gradle 依赖: ```groovy implementation 'org.freemarker:freemarker:2.3.31' ``` 接下来,我们可以编写 Freemarker 模板文件 `template.ftl`,用于生成 XML。 示例模板文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <root> <user id="${user.id}" name="${user.name}"> <age>${user.age}</age> </user> </root> ``` 然后,我们可以编写 Java 代码,使用 Freemarker 解析模板文件,并将所需数据填充到模板中,生成 XML 文件。 示例 Java 代码: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FreemarkerXmlGenerator { public static void main(String[] args) throws IOException, TemplateException { // 创建 Configuration 实例 Configuration configuration = new Configuration(Configuration.getVersion()); // 设置模板文件所在目录 configuration.setDirectoryForTemplateLoading(new File("/path/to/template/dir")); // 加载模板文件 Template template = configuration.getTemplate("template.ftl"); // 准备数据 Map<String, Object> data = new HashMap<>(); data.put("user", new User(1, "Tom", 20)); // 创建输出文件 File outputFile = new File("/path/to/output/file.xml"); FileWriter fileWriter = new FileWriter(outputFile); // 填充数据并生成 XML 文件 template.process(data, fileWriter); // 关闭输出流 fileWriter.close(); } private static class User { private int id; private String name; private int age; public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } } } ``` 以上代码中,我们通过 `Configuration` 类加载模板文件,然后用 `Template` 类进行解析和填充,并将结果输出到指定的文件中。注意要在最后关闭输出流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值