SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I ...?


How do I ...?

Author
Message
rightontarget
rightontarget
SSC Veteran
SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)

Group: General Forum Members
Points: 243 Visits: 468
Hi all,
I know nothing about processing an XML file in SQLServer.
What I do know is: I have an xml file on the network and I need to retrieve some data from it.
The file structure is:


<?xml version="1.0" encoding="utf-8" ?>
<Cfo>
<Env>T3</Env>
<Out>D:\Out</Out>
<Comp>
<Comp>
<Comp>
<Comp>
<Rev>
<Dep>12345</Dep>
<DepDate>Wed, 08 May 2013 14:01:29 GMT</DepDate>
</Rev>
</Cfo>


Looking for your help and guidance on how to approach this. Do I need to load xml file into a table first?
How do I retrieve values from <Env> and <Rev> nodes???

Thanks,
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3184 Visits: 1779
You can store it in a table or a variable and work with it ok. It just depends what you need to do with the XML.. Once you have shredded the XML, do you need it anymore? if not then storing it in a variable will be just fine..

Here is a way to get the XML from file into a variable to work with it:

DECLARE @xml XML
SELECT @xml = (SELECT * FROM OPENROWSET(BULK 'c:\Temp\SomeXmlFile.Xml', SINGLE_BLOB) x)



The filepath does need to be relative to the SQL server and the account that the SQL server service runs under will need to have permissions to access the file. If you are running SQL server under the localsystem account, then I don't believe it will be able to access the file over the network.

Once you have it in a variable, then you can use the XML methods to begin to shred it. The most simplest one is value(). Another good one which helps with shredding XML to a table is nodes(). There are loads of blog posts on shredding XML to a table using nodes(). I've done a couple myself Smile

Here is a simple example using the value() method:


SELECT @xml.value('(/Cfo/Env/text())[1]', 'varchar(20)')


rightontarget
rightontarget
SSC Veteran
SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)

Group: General Forum Members
Points: 243 Visits: 468
Thank you very much, it worked for me.
What is your blog URL?
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3184 Visits: 1779
rightontarget (5/10/2013)
Thank you very much, it worked for me.
What is your blog URL?


Thanks. glad it helped

my blog is syndicated right here at SSC http://www.sqlservercentral.com/blogs/rocks/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search