title: Freemarker - generate xml from flt template and java methods
body: <p>I am fairly new to Freemarker and to template engine in general.
  Basicly I want to be able to generate two XML files (from two different templates) using java methods to feed data to the template. </p>
  
  <p>For example:</p>
  
  <ul>
  <li>myTemplate1.ftl contain an opening and closing "res" tag with <code>getRes()</code> method as value (return 1 for exemple)</li>
  <li>myTemplate2.ftl contain an opening and closing "result" tag and takes <code>getResult()</code> method as value (return 2 for exemple)</li>
  </ul>
  
  <p>How can I write one class <code>Main</code> to process this without being specific (I do not want to write line per line, because then it would be pointless to create an engine)</p>
  
  <p>Can you please help me understand how it work through an example if possible ?</p>
  
  <p>Regards</p>
  
  <p><strong>EDIT with all the new informations :</strong></p>
  
  <p>@fustaki This is very frustrating. It seems I need to go step by step to understand what I'm doing. So... Here is a very dumb example of where I am :</p>
  
  <p>template1.ftl</p>
  
  <pre><code>&lt;Email&gt;${item.getEmail()}&lt;/Email&gt;&lt;Language&gt;${item.getLanguage()}&lt;/Language&gt;
  </code></pre>
  
  <p>FillMyTemplate1.java</p>
  
  <pre><code>public String getEmail(){ return "test@test.com" }
  public String getLanguage(){ return "EN" }
  </code></pre>
  
  <p>I am using a property file in order to use introspection : prop.properties which contain : 
      template1=fr.freemarker.test.xml.FillMyTemplate1</p>
  
  <p>MainTest.java</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
   Properties prop = new Properties();
   InputStream input = new FileInputStream("prop.properties");
   prop.load(input);
   Class&lt;?&gt; classe = Class.forName(prop.getProperty(args[0])); //where args[0] is "template1"
  
   Configuration config=new Configuration();
   config.setClassForTemplateLoading(MainTest.class, "templates");
   config.setObjectWrapper(new DefaultObjectWrapper());
   Template template=config.getTemplate(args[0]+".ftl");
  
   Map&lt;String, Object&gt; dataModel = new HashMap&lt;String, Object&gt;();
   Writer consoleWriter = new OutputStreamWriter(System.out);
   dataModel.put("item", classe.newInstance());
   template.process(dataModel, consoleWriter);
  </code></pre>
  
  <p>}</p>
  
  <p>And the result : <code>&lt;Email&gt;test@test.com&lt;/Email&gt;&lt;Language&gt;EN&lt;/Language&gt;</code></p>
  
  <p>In order to understand what you said, I need to know how a "data-provider" would look like in my case ? How can I get ride of this "item" in my map ? I understand the principle but not the technical solution...</p>