前言
1、Freemarker解析模板
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-freemarker</ artifactId>
</ dependency>
1.1、ftl模板
<?xml version="1.0" encoding="utf-8"?>
< root id = " " comment = " " >
< person>
< person> ${x.person}</ person>
< age> ${x.age}</ age>
< name> ${name}</ name>
< sex> ${x.sex}</ sex>
</ person>
</ root>
1.1.2、制作map
1.1.2.1、DTO
@Data
public class XmlEntry {
public String person ;
public Integer age;
public Integer sex;
public String name ;
public double salary;
public Integer classPersonNum;
}
1.1.2.2、DTO
private static Map demo ( ) {
XmlEntry p = new XmlEntry ( ) ;
p. setAge ( 24 ) ;
p. setPerson ( "张宇晋" ) ;
p. setSex ( 1 ) ;
Map map = new HashMap ( ) ;
map. put ( "name" , "zhangyujin" ) ;
map. put ( "x" , p) ;
return map;
}
1.2.3、根据不同模板目录生成文本
public static String createXmlFile ( String TemplateName, Map dataMap) throws TemplateException, IOException {
Configuration cfg = new Configuration ( ) ;
cfg. setEncoding ( Locale. CHINA, "UTF-8" ) ;
File file = new File ( CreateXmlByVmFile. class . getResource ( "" ) . getFile ( ) ) ;
cfg. setDirectoryForTemplateLoading ( file) ;
Template template = cfg. getTemplate ( TemplateName, "utf-8" ) ;
StringWriter w = new