This post demonstrates how to render an XML to a file from a given XML template using Spring Boot and Thymeleaf.
No Web
If you are not going to use a web container for your project, you may exclude the embedded Tomcat libraries from it.
Please see Exclude Embedded Tomcat in Spring Boot when using Thymeleaf.
XML Template
You have an XML template with placeholders and you have it transformed or rendered with appropriate values.
Your Template
The following XML template has three (3) placeholders – lastname, firstname, and country.
1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="UTF-8"?> <persons> <person> <fname th:text="${pinfo['lastname']}"></fname> <lname th:text="${pinfo['firstname']}"></lname> <country th:text="${pinfo['country']}"></country> </person> </persons> |
Spring Beans
You’ll need two @Bean’s – SpringResourceTemplateResolver and SpringTemplateEngine.
Using SpringResourceTemplateResolver
This bean configures the Thymeleaf template resolver.
1 2 3 4 5 6 7 8 9 10 11 | @Bean SpringResourceTemplateResolver xmlTemplateResolver(ApplicationContext appCtx) { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(appCtx); templateResolver.setPrefix("classpath:/templates/"); templateResolver.setSuffix(".xml"); templateResolver.setTemplateMode("XML"); templateResolver.setCharacterEncoding("UTF-8"); templateResolver.setCacheable(false); return templateResolver; } |
Using SpringTemplateEngine
This bean simply sets the template resolver to the template engine. The resolver will be responsible for figuring out retrieve the template.
1 2 3 4 5 6 | @Bean(name="springTemplateEngine") SpringTemplateEngine templateEngine(ApplicationContext appCtx) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(xmlTemplateResolver(appCtx)); return templateEngine; } |
The main class
This is where we supply values and invoke the template engine to use a particular XML template and render it with those values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.turreta; import java.util.HashMap; import java.util.Map; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.thymeleaf.context.Context; import org.thymeleaf.spring4.SpringTemplateEngine; @SpringBootApplication public class ThymeleafDemoApplication { public static void main(String[] args) { ApplicationContext appContext = SpringApplication.run(ThymeleafDemoApplication.class, args); // We used "springTemplateEngine" to name our SpringTemplateEngine SpringTemplateEngine engine = (SpringTemplateEngine)appContext.getBean("springTemplateEngine"); Map<String, String> pinfo = new HashMap<>(); Context context = new Context(); context.setVariable("pinfo", pinfo); pinfo.put("lastname", "Jordan"); pinfo.put("firstname", "Michael"); pinfo.put("country", "USA"); /* * Use a particular template passing the context that has * the values for the place-holders. */ String content = engine.process("person-details", context); // Rendered XML System.out.println(content); } } |
Download the codes
Please download or fork the codes from https://github.com/Turreta/thymeleaf-render-xml-01