Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

newbie - parse filepath field into xml doc

 
   Database Help (Home) -> XML RSS
Next:  The order of processing a SQL statement  
Author Message
Mike Stokan

External


Since: Dec 04, 2008
Posts: 1



(Msg. 1) Posted: Thu Dec 04, 2008 1:43 pm
Post subject: newbie - parse filepath field into xml doc
Archived from groups: microsoft>public>sqlserver>xml (more info?)

I have a SQL 2005 table that stores filenames with attached path. Could
someone point me to a way to parse the path/filename into an XML hierarchy?

Many thanks.

mike

 >> Stay informed about: newbie - parse filepath field into xml doc 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 2) Posted: Fri Dec 05, 2008 12:34 am
Post subject: RE: newbie - parse filepath field into xml doc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

This is a rough example but hopefully you get the idea:

USE master
GO

-- Modified from
http://www.microsoft.com/communities/newsgroups/list/en-us/default.asp...uery=sp
IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'dbo.Split') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.Split
GO

CREATE FUNCTION dbo.Split
(
@str VARCHAR(MAX),
@separator VARCHAR(MAX)
)
RETURNS @tableOutput TABLE ( row_id INT IDENTITY PRIMARY KEY, result
VARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML;

SET @xml = CAST( ' <tag>' ) +
'</tag>' AS XML )

INSERT @tableOutput ( result )
SELECT T.i.value('.', 'VARCHAR(255)') result
FROM @xml.nodes('tag') T(i);

RETURN
END
GO

USE tempdb
GO

DROP TABLE #tmp
CREATE TABLE #tmp( file_id INT IDENTITY UNIQUE, filepath VARCHAR(255)
PRIMARY KEY )
GO

INSERT INTO #tmp VALUES ( 'c:\temp\' )
INSERT INTO #tmp VALUES ( 'c:\temp\temp.txt' )
INSERT INTO #tmp VALUES ( 'c:\temp\temp2\temp.csv' )
GO

-- Parse them out into separate components
-- drive, folder1, folder2, full filename, filename only, extension
SELECT file_id, result AS filepath_element
FROM #tmp t
CROSS APPLY master.dbo.Split( t.filepath, '\' )
FOR XML RAW
GO

HTH
wBob

"Mike Stokan" wrote:

> I have a SQL 2005 table that stores filenames with attached path. Could
> someone point me to a way to parse the path/filename into an XML hierarchy?
>
> Many thanks.
>
> mike
>
>
>

 >> Stay informed about: newbie - parse filepath field into xml doc 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 3) Posted: Fri Dec 05, 2008 5:18 am
Post subject: RE: newbie - parse filepath field into xml doc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Just a quick note about the script, the

-- Parse them out into separate components
-- drive, folder1, folder2, full filename, filename only, extension

comment at the bottom was actually a TODO, not what the script really does ...

"Bob" wrote:

> This is a rough example but hopefully you get the idea:
>
> USE master
> GO
>
> -- Modified from
> http://www.microsoft.com/communities/newsgroups/list/en-us/default.asp...uery=sp
> IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
> OBJECT_ID(N'dbo.Split') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
> DROP FUNCTION dbo.Split
> GO
>
> CREATE FUNCTION dbo.Split
> (
> @str VARCHAR(MAX),
> @separator VARCHAR(MAX)
> )
> RETURNS @tableOutput TABLE ( row_id INT IDENTITY PRIMARY KEY, result
> VARCHAR(MAX))
> AS
> BEGIN
> DECLARE @xml XML;
>
> SET @xml = CAST( ' <tag>' ) +
> '</tag>' AS XML )
>
> INSERT @tableOutput ( result )
> SELECT T.i.value('.', 'VARCHAR(255)') result
> FROM @xml.nodes('tag') T(i);
>
> RETURN
> END
> GO
>
> USE tempdb
> GO
>
> DROP TABLE #tmp
> CREATE TABLE #tmp( file_id INT IDENTITY UNIQUE, filepath VARCHAR(255)
> PRIMARY KEY )
> GO
>
> INSERT INTO #tmp VALUES ( 'c:\temp\' )
> INSERT INTO #tmp VALUES ( 'c:\temp\temp.txt' )
> INSERT INTO #tmp VALUES ( 'c:\temp\temp2\temp.csv' )
> GO
>
> -- Parse them out into separate components
> -- drive, folder1, folder2, full filename, filename only, extension
> SELECT file_id, result AS filepath_element
> FROM #tmp t
> CROSS APPLY master.dbo.Split( t.filepath, '\' )
> FOR XML RAW
> GO
>
> HTH
> wBob
>
> "Mike Stokan" wrote:
>
> > I have a SQL 2005 table that stores filenames with attached path. Could
> > someone point me to a way to parse the path/filename into an XML hierarchy?
> >
> > Many thanks.
> >
> > mike
> >
> >
> >
 >> Stay informed about: newbie - parse filepath field into xml doc 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Newbie - generate XML tree from parent-child hierarchy - Hi all, I'm trying to do something that I know should be straightforward, but I just cannot get it to work. Imagine a simple Bill-Of-Materials with one table and a parent-child relationship. e.g. PartNumberID, ParentPartNumberID, PartDescription,..

newbie to xpath/xquery trying to pull email address out of.. - i'm trying to pull out email addresses info from the SSRS subscription table. The xml looks like this: <ParameterValues> <ParameterValue> <Name>TO</Name> <Value>pep1@myemail.com;pep2@myemail.com</Value> </Pa...

field values as element name - I am using SQL Server 2000 and need to generate an XML file from a database table. Basically I need to generate an XML file from two fields in a table called MyTable. I don't need to include all fields from this table. One field is ContentDescriptio...

Null Field Values not returned - For XML AUTO - Is there a way to get SQL to return NULL field values as 'empty' XML tags, instead of completely omitting them from the output? I'm using FOR XML AUTO

Update DB Date Field when importing xml with SQLXML - I'm importing xml files into a relational database using SQLXML through C# into SQLExpress I have the data going in ok but the tables have Date fields I'd like to add with each record. This field is set to NOT NULL. Obviously, with this field set to....
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]