Software Development, Spring

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.

Using SpringResourceTemplateResolver

This bean configures the Thymeleaf template resolver.

Using SpringTemplateEngine

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

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.

Download the codes

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

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like