Some time ago I have decided to go a little deeper into SQL. And by some time ago I probably mean some years. Anyhow, in this article I will present the ways I know to make XML out of SQL tables.
I am using the following db here. It contains of two tables, the one about the football players info and the other for some info, concerning their cities. Something like this:
So, these are the three methods I know:
Using Elements RAW
This method returns RAW XML from the tables. Pretty much something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<FootballInfo> <row> <ID>1</ID> <FirstName>Peter</FirstName> <LastName>The Keeper</LastName> <Club>NorthClub</Club> <CityID>1</CityID> </row> <row> <ID>2</ID> <FirstName>Ivan</FirstName> <LastName>Mitov</LastName> <Club>EastClub</Club> <CityID>2</CityID> </row> <row> <ID>3</ID> <FirstName>Stoyan</FirstName> <LastName>Mitkov</LastName> <Club>EastClub</Club> <CityID>3</CityID> </row> <row> <ID>4</ID> <FirstName>Vitosh</FirstName> <LastName>Doynov</LastName> <Club>EastClub</Club> <CityID>1</CityID> </row> <row> <ID>5</ID> <FirstName>Pesho</FirstName> <LastName>Stoyanov</LastName> <Club>SouthClub</Club> <CityID>3</CityID> </row> <row> <ID>6</ID> <FirstName>Gosho</FirstName> <LastName>Kiriyakov</LastName> <Club>SouthClub</Club> <CityID>1</CityID> </row> <row> <ID>7</ID> <FirstName>Haralampi</FirstName> <LastName>Iliev</LastName> <Club>SouthClub</Club> <CityID>1</CityID> </row> </FootballInfo> |
In order to get this one, we need to call the following:
1 2 |
select * from Footballers for xml raw, elements,root('FootballInfo') |
Depending on the presence or absence of “elements” and “root” the results may vary.
Using Elements AUTO
Elements auto is a bit more clever. It takes a look at the joined tables and gives us XML, which is based on it. Thus this query:
1 2 3 4 |
use VitoshAcademyCom select Footballers.firstname, Footballers.lastname, Footballers.Club, CityInfo.City, CityInfo.boss from Footballers inner join CityInfo on CityInfo.id = Footballers.Cityid for xml auto, root('FootballInfo') |
returns the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<FootballInfo> <Footballers firstname="Peter" lastname="The Keeper" Club="NorthClub"> <CityInfo City="Sofia" boss="Vitosh" /> </Footballers> <Footballers firstname="Ivan" lastname="Mitov" Club="EastClub"> <CityInfo City="Plovdiv" boss="Vityata" /> </Footballers> <Footballers firstname="Stoyan" lastname="Mitkov" Club="EastClub"> <CityInfo City="Varna" boss="Vito" /> </Footballers> <Footballers firstname="Vitosh" lastname="Doynov" Club="EastClub"> <CityInfo City="Sofia" boss="Vitosh" /> </Footballers> <Footballers firstname="Pesho" lastname="Stoyanov" Club="SouthClub"> <CityInfo City="Varna" boss="Vito" /> </Footballers> <Footballers firstname="Gosho" lastname="Kiriyakov" Club="SouthClub"> <CityInfo City="Sofia" boss="Vitosh" /> </Footballers> <Footballers firstname="Haralampi" lastname="Iliev" Club="SouthClub"> <CityInfo City="Sofia" boss="Vitosh" /> </Footballers> </FootballInfo> |
Using Elements PATH
Element Path is the most sophisticated way of turning sql tables to xml. In it, we may define our own elements in the XML (e.g. the path) like this:
1 2 3 4 |
use VitoshAcademyCom select Footballers.firstname 'Names/FirstName', Footballers.lastname 'Names/LastName', Footballers.Club, CityInfo.City 'CityInfo/Name', CityInfo.boss 'CityInfo/Boss' from Footballers inner join CityInfo on CityInfo.id = Footballers.Cityid for xml path('Information'), root('FootballInfo') |
Thus, we would get a new element called Names, which would have the properties FirstName and LastName like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<FootballInfo> <Information> <Names> <FirstName>Peter</FirstName> <LastName>The Keeper</LastName> </Names> <Club>NorthClub</Club> <CityInfo> <Name>Sofia</Name> <Boss>Vitosh</Boss> </CityInfo> </Information> <Information> <Names> <FirstName>Ivan</FirstName> <LastName>Mitov</LastName> </Names> <Club>EastClub</Club> <CityInfo> <Name>Plovdiv</Name> <Boss>Vityata</Boss> </CityInfo> </Information> <Information> <Names> <FirstName>Stoyan</FirstName> <LastName>Mitkov</LastName> </Names> <Club>EastClub</Club> <CityInfo> <Name>Varna</Name> <Boss>Vito</Boss> </CityInfo> </Information> <Information> <Names> <FirstName>Vitosh</FirstName> <LastName>Doynov</LastName> </Names> <Club>EastClub</Club> <CityInfo> <Name>Sofia</Name> <Boss>Vitosh</Boss> </CityInfo> </Information> <Information> <Names> <FirstName>Pesho</FirstName> <LastName>Stoyanov</LastName> </Names> <Club>SouthClub</Club> <CityInfo> <Name>Varna</Name> <Boss>Vito</Boss> </CityInfo> </Information> <Information> <Names> <FirstName>Gosho</FirstName> <LastName>Kiriyakov</LastName> </Names> <Club>SouthClub</Club> <CityInfo> <Name>Sofia</Name> <Boss>Vitosh</Boss> </CityInfo> </Information> <Information> <Names> <FirstName>Haralampi</FirstName> <LastName>Iliev</LastName> </Names> <Club>SouthClub</Club> <CityInfo> <Name>Sofia</Name> <Boss>Vitosh</Boss> </CityInfo> </Information> </FootballInfo> |
Pretty much that’s all. If you want more, take a look at my GitHub account here.