FreeMarker Build XML

FreeMarker Build XML

I always prefer Java template, if any kind of predefined data structure need to be build. For example if you want to send email to somebody in HTML format with style and color as you have seen in this tutorial using Apache velocity template or if you have to create predefined XML file with dynamic data. Here I will show you how you can iterate java object (Peerson) and dynamically create XML file from FreeMarker template.

  • Project structure:

FreeMarker Build XML

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javahonk</groupId>
	<artifactId>FreeMarkerCreateXMLFile</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>FreeMarkerCreateXMLFile</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.22</version>
		</dependency>
	</dependencies>
</project>
  • BuildXMLTemplate.ftl
<Persons>
	<Message>FreeMarker Template build XML: ${name}</Message>
	
	<#list personDetails as person>
		<Person>	
			<Name>${person.name}</Name>
			<Location>${person.location}</Location>
		</Person>
	</#list>
</Persons>
 



  • Person.java – This is main class where we will store all XML data in list and dynamically replace its value to the template and create XML file.
package com.javahonk;

import java.io.Serializable;

public class Person implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private String name;
	private String location;
	
	public Person(String name, String location) {
		super();
		this.name = name;
		this.location = location;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}

}
  • FreeMarkerGenrateXMLFromObject.java:
package com.javahonk;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

public class FreeMarkerGenrateXMLFromObject {
	
	private static Configuration cfg = null;
	
	static{		
		// Freemarker below configuration object deprecated
		//Configuration cfg = new Configuration();
		//Please use this. To make it backward compatible. Please visit here for more info: 
		//http://freemarker.org/docs/api/freemarker/template/Configuration.html
		cfg = new Configuration(new Version("2.3.0"));
	}
	
	public static void main(String[] args) {

		try {
			// Load template
			Template template = cfg.getTemplate("resources/BuildXMLTemplate.ftl");

			// Create data for template
			Map<String, Object> templateData = new HashMap<String, Object>();
			templateData.put("name", "Java Honk");
			
			List<Person> personDetails = Arrays.asList(
				       new Person( "Java Honk", "USA" ), 
				       new Person( "Java Monk", "France" ) );

			templateData.put("personDetails", personDetails);

			// Write output on console example 1
			StringWriter out = new StringWriter();
			template.process(templateData, out);
			System.out.println( out.getBuffer().toString() );
			out.flush();
			
			// Write output on console example 2
			/*Writer out = new OutputStreamWriter(System.out);
			template.process(templateData, out);
			out.flush();*/

			// Write data to the file
			Writer file = new FileWriter(new File("C:\\JavaHonk\\BuildXMLTemplateXML.xml"));
			template.process(templateData, file);
			file.flush();
			file.close();

		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}
  • Output:

FreeMarker Build XML

FreeMarker Build XML

  • For more details please visit FreeMarker official tutorial here

download  Download Project:  FreeMarkerCreateXMLFile

Leave a Reply

Your email address will not be published. Required fields are marked *