Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How do I ...? Expand / Collapse
Author
Message
Posted Thursday, May 09, 2013 10:07 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:51 PM
Points: 89, Visits: 300
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,
Post #1451237
Posted Friday, May 10, 2013 1:36 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:36 AM
Points: 2,418, Visits: 1,565
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 :)

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

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

Post #1451456
Posted Friday, May 10, 2013 3:23 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:51 PM
Points: 89, Visits: 300
Thank you very much, it worked for me.
What is your blog URL?
Post #1451749
Posted Saturday, May 11, 2013 12:56 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:36 AM
Points: 2,418, Visits: 1,565
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/
Post #1451812
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse