0

Render XML using Thymeleaf

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.

Spring Beans

You’ll need two @Bean’s – SpringResourceTemplateResolver and SpringTemplateEngine.

SpringResourceTemplateResolver

This bean configures Thymeleaf template resolver.

SpringTemplateEngine

This bean simply sets the template resolver to template engine. The resolver will be responsible in figuring out retrieve the template.

The main class

This is where we supply values and invoke the template engine to use particular XML template and render it with those values.

Download the codes

Please download or fork the codes from https://github.com/Turreta/thymeleaf-render-xml-01

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert