I am in a project in which I want to display the data fetched from a data base. I use a java bean to get the data and pass it to a JSP. From that I want to make use of XML technology to print the data to the user browser. I am using an XSL file. After getting the data from the data base how can I manage that into an XML file? Do I have to create an XML file dynamically? If so should I have to store it on the disk? And how can I bind an XML file to a XSL file? I want to change the style sheet (XSL) each time.

Thijs Stalenhoef

1. Data from database to XML: Depends on the complexity of your data. A simple table is easily convert like:


When you have linked tables use the primary keys to creates a more hierarchical representation:

<field2fromtable1>         (suppose this is primary key)
     etc. etc.

For more a more indepth discussion take a look at The XML Handbook or a similar publication.

2. Should I create XML dynamically?

Some databases can generate XML directly (Oracle 8i being one of them). The method described above can be used when your database doesn't do this.

3. Store it on disk yes/no?

Storing it on disk will of course increase performance but lose flexibility. In other words, if you store in on disk you'll have to check every time whether or not the data you need has changed in the database. If so you'll have to generate the XML file again. If your conversion code is fast enough you don't have to save it into a file everytime. You might consider doing it anyway as a form of result caching though.

4. Binding XML to XSL

Just make your XML header look something like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="somestylesheet.xsl" ?>

The second line make the reference to the stylesheet you want to use. When given an XML containing this line IE5 for instance will simply get the XSL in question and use it to transform the XML for you. You can change this line everytime you output the XML. If you are serving up XML from disk you might want to leave out the header and add it at the time you are serving it to the client. A simple servlet should do the trick.