Step 3:
Profit!
Seriously, you would build up an in-memory DOM using the data you retrieve, then serialize it to the XML file. I am assuming that you are also planning on auto-generating the element names from database and column metadata?
In any case, this is much more of an XML issue than a
JDBC one, so I'm going to transfer this over to the XML forum for further discussion.