I wrote earlier about using open source software for fulfilling functionality prior to building a custom implementation. Many projects have the need to transform object to text or XML – e.g. return XML response for REST-based services or return JSON string to support a web application etc.
Freemarker is a templating engine that can be leveraged to perform object to text transformations – with freemarker it is easy to generate RSS feeds or delimited text or plain old XML very easily. Below are some code fragments that illustrate freemarker usage:
Below is a code fragment for setting up the template configuration:
try {
// Initialize configuration;
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(templatesLocation));
cfg.setTemplateUpdateDelay(0);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
//Use beans wrapper (recommmended for most applications)
cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
cfg.setDefaultEncoding("ISO-8859-1");
//charset of the output
cfg.setOutputEncoding("UTF-8");
//default locale
cfg.setLocale(Locale.US);
} catch (Exception e) {
//handle the exception
}
The templatesLocation variable is the path to a folder with freemarker templates (referred to as FTL templates). These are similar to templates used with frameworks such as Velocity. For example, below is a template fragment:
<Name>${myBean.firstName?xml} ${myBean.lastName?xml}</Name>
“myBean” is the java object that has first name and last name properties supported by JavaBeans style getXYZ() methods. The ?xml built-in function is used to encode any special XML characters.
Below is the code fragment when trying to invoke a template with a java object – the template file name is hard-coded for below example – it can be fetched from an input parameter or injected directly into this object as necessary:
// loading the template file String templateFileName = "test-template.ftl"; Template template = cfg.getTemplate(templateFileName); Writer out = new StringWriter(); template.process(dataObject, out); String strResponse = out.toString();
The above code fragment essentially takes a java object (dataObject) as input and creates a string as output. Using the template above, the output for example could be:
<Name>John Smith</Name>
Freemarker also supports loops and conditions and offers a rich library of built-in functions as well.





[...] RSS feeds using Freemarker As a follow up to the earlier post on using freemarker, here is an example of creating RSS 2.0 feeds using a freemarker template. This [...]
the correct template should look like this
${myBean.firstName?xml} ${myBean.lastName?xml}
sure, that it is pretty unlikely, that a name contains any of the special xml characters, but who knows…
Thanks André you are right! have updated the sample code snippet – if we are transforming data that has poor data quality controls, special characters are certainly a possibility.
Hi, I was wondering if you created a new class for the java-Bean with getter and setters? If so will you share the code how you parse that with the template?
Didn’t create a new class – just pass the template file name to the Configuration object to get the appropriate Template object instance. Have updated the original code to reflect this.