Convert XML to HTML using XSL in Java
This short tutorial explains how to convert XML to HTML applying XSL in Java. The scenario considered
here is generating a restaurant menu.
Let us assume the restaurant menu is dynamic in nature but does not change very often.
So for each request it does not make sense to hit the database and construct the menu.
So when ever the restaurant owner changes the menu [using Admin module], we generate a HTML page
from XML applying XSL transformation.
The code which does this is very simple and as mentioned below.
The imported Java files are as in the screen shot below [which were collapsed in the previous screen shot].
In this example
menu.html is generated as the output of the program execution.
menu.xml contains the restaurant menu data and
menu.xsl [template.xsl is referred by menu.xsl]
the required XSL transformation.
Download the source
to try it yourself. [The resultant menu.html does not look good as it requires a CSS and few images which are
not ptovided in the zip. However it is good enough for the demo].
Note: Please change the location of the xml and xsl files as per your requirement.