***I’ve been having difficulty getting the code to display correctly. NOTE: the xml code is case sensitive! I’ve been trying to get all the bugs this lovely blog template is throwing at me, but I’m sorry if i miss some.***
This is an easy way to get xml into a relational table to work a little easier with other tables in the database. There are a few ways out there that produce the same result, but it seemed like this was the simplest to work with.
declare @xml xml
set @xml =
'~~?xml version="1.0" encoding=''UTF-8'' ?~~
-
-
-
-
-
'
select c.value('@question[1]','varchar(255)') as Question
,c.value('@answer[1]','int') as Answer
from @xml.nodes('//rows/item') as t(c)
Because of how the blog parses things, replace ~~ with the respective greater than or less than brackets.
The query is parsing everything for you and putting into a table. The example is using attributes for the query statement. Its a little easier on the eyes for me when trying to read what the results are expected to be, but I do not work with xml that much.
In the time I’ve been playing with it, it seems the default way is to use elements exclusively. As this example shows:
declare @xml xml
set @xml =
'~~?xml version="1.0" encoding=''UTF-8'' ?~~
-
name1
1
-
name2
2
-
name3
3
-
name4
4
-
name5
5
'
select c.value('question[1]','varchar(255)') as Question
,c.value('answer[1]','int') as Answer
from @xml.nodes('//rows/Item') as t(c)
Besides the format change, the only thing that really changed in the query is I told it to look for elements. This is represented by the change from ‘@question[1]‘ to ‘question[1]‘. A small change, but this little small change can be really frustrating to try to figure out if you don’t know about it.
The just element route bugged me in trying to explain things, cuz everything I’ve been dealing with has a mix of elements and attributes. So getting confused on how to call it was the hardest step trying to reverse engineer my findings from internet blogs.The big thing to remember is attributes are called with an @ symbol before the attribute name, and the element is called with just the element name.
The value that is in the brackets is what date you are trying to grab. Since there is only one item in the tree, this makes it easy to grab the first dataset… I’m sorry if I’m giving bad terminology, I’m a little new towards what to properly call it.
If there is a situation where you want only the second dataset as this example:
-
name1
1
name6
6
Querying with the value of ‘question[2]‘ will return the Question:name6, answer:6 as a row in the table. However, with the limited playing I have done, i don’t know how to only tell it to go for the 2nd data set for a particular element. With the code as ‘question[2]‘, it is going to return all the second “Items.” Something to play around with for sure.
The data type after which attribute/element you are querying is what you need to convert to for the table to display. You want to make sure you have compatible data types. They use SQL data types, so stick to the minimal data types you are expecting.
To specify where you want to look in the xml, the @xml.nodes is where you do it. In the example above, I have the root as rows and the first element as Item. Can’t get much easier that that.
