I have a query that when run returns a resultset with the following data in the order and grouping shown:
Country Region Town
---------------------------------------
England North NewCastle
England North Manchester
England North Leeds
England South London
England South Bristol
England South Birmingham
England South Portsmouth
Norway North Trondheim
Norway North Tromso
Norway South Oslo
Norway South Stavanger
Norway West Bergen
Using Java, i would like to convert the returned result into an XML document as shown below:
<countries>
<country>
<countryName>England</countryName>
<region name = "south">
<town>London</town>
<town>Bristol</town>
<town>Birmingham</town>
<town>Portsmouth</town>
</region>
<region name = "north">
<town>NewCastle</town>
<town>Leeds</town>
</region>
<country>
<country>
<countryName>Norway</countryName>
<region name = "south">
<town>Oslo</town>
<town>Stavanger</town>
</region>
<region name = "west">
<town>Bergen</town>
</region>
<region name = "North">
<town>Trondheim</town>
<town>Tromso</town>
</region>
<country>
<countries>
What is the best way to traverse the data so that the tags are created and closed at the correct position? I have seen an example here http://www.mkyong.com/java/how-to-create-xml-file-in-java-jdom-parser/ but the structure of the data is flat unlike the sample i am using which will probably require multiple loops.