March 28, 2006

Try using FreeMarker to generate XML documents

Generating XML from code is a very common development task. There are a multitude of ways it can be accomplished. Recently, at a client, we needed to generate an XML message which had to conform to a rather complicated XML schema. We already had an object model that worked well with our code base. We considered using XML binding API's like:
Each of these had its own limitations which reduced their feasibility within our application. Next we considered hand-coding the XML directly with API's like:
The amount of code required to create an XML document with these API's was excessive. Code like this was not fun to write nor easy to follow.

So we decided to take a different approach and use a templating engine, specifically FreeMarker. Using FreeMarker to generate the XML response messages turned out to be a great decision. This is a list of some of the advantages we have noticed:
  1. The FreeMarker template we wrote to create the XML response message is an XML document. This simple fact should not be overlooked.
  2. Easy to integrate into any application. Very little code was required to utilize FreeMarker in our application.
  3. Excellent performance.
  4. The built-in functionality that FreeMarker provides is very powerful.

3 comments:

Jason Yip said...

Although we haven't had to implement the feature yet, we had the same idea on the project I'm on. Glad to see that it worked out.

Anonymous said...

Hi can you please post it how you used freemaarker to generate the xmal message.

Michael Ward said...

Sorry I used this on a client a few years back and don't have access to the code.