SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Extracting XML nodes
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Clages1
Yak Posting Veteran

67 Posts

Posted - 01/09/2013 :  07:11:49  Show Profile  Reply with Quote
Hi
i have a TXT file in fact a XML file
i would like thru SQL read this XML file and
extract piece of information.

sample Xml file below

<Person>John Doe</Person>
<Place>Seattle, WA</Place>

i would like to read it and pass 2 parameters
<Place> and </Place>
and get "Seattle, WA"

i am reading
http://www.sqlservercentral.com/stairway/92778/
but i did not find anything about READING and Extracting nodes

any help will be apreciated
tks
Clages





sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 01/09/2013 :  07:41:51  Show Profile  Reply with Quote
The second parameter (</Place>) is redundant, so you need only one parameter. Also, it is easier if you just pass "Place" rather than "<Place>". Assuming so, here is an example of how you can query:
DECLARE @param VARCHAR(32) 
SET @param = 'Place'
DECLARE @x XML;
SET @x  = 
	'<Person>John Doe</Person>
	<Place>Seattle, WA</Place>';

SELECT
	c.query('.')
FROM
	@x.nodes('/*[local-name() = sql:variable("@param")]') T(c)


This should work in SQL 2005 and above - I am not familiar with the capabilities of Sql XML in SQL 2000, so the this may not work on SQL 2000.

Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
651 Posts

Posted - 01/09/2013 :  19:50:47  Show Profile  Reply with Quote
quote:
Originally posted by Clages1

Hi
i have a TXT file in fact a XML file
i would like thru SQL read this XML file and
extract piece of information.

sample Xml file below

<Person>John Doe</Person>
<Place>Seattle, WA</Place>

i would like to read it and pass 2 parameters
<Place> and </Place>
and get "Seattle, WA"

i am reading
http://www.sqlservercentral.com/stairway/92778/
but i did not find anything about READING and Extracting nodes

any help will be apreciated
tks
Clages


Hold the phone a minute. Let's find something out, first.
  1. Are you saying that you have a file on the disk that you'd first like to import into SQL Server?
  2. Are you really using SQL Server 2000 for this?
--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."

Edited by - Jeff Moden on 01/09/2013 19:54:29
Go to Top of Page

Clages1
Yak Posting Veteran

67 Posts

Posted - 01/10/2013 :  12:36:44  Show Profile  Reply with Quote
Hi , I am using SQL2008
the sample of our friend sunitabeck
doesnt work, because it no using a XML file as Input

the main Ideia is,
pass Xml file as Parameter , Node as parameter
then read this XML file and get String between Nodes.

SOmething like this

SPgetnodes(c:\xxxx\nfe.xml, "<Place>")
return "Seattle, WA"


this below is a sample XML file

<Person>John Doe</Person>
<Place>Seattle, WA</Place>


tks

Carlos Lages
Brazil
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 01/10/2013 :  15:56:43  Show Profile  Reply with Quote
SQL Server and T-SQL may not be the best tool for the job if your objective is to parse files and extract data.

If you don't need the results in a T-SQL script, consider using a .Net program.

If you do need to get the result inside a T-SQL script, you would need to import the file into a database table using BCP, SSIS or other similar tools and then query against that table.

Other alternatives might be to use a CLR stored proc, or xp_cmdshell in combination with a .Net program.

Go to Top of Page

Clages1
Yak Posting Veteran

67 Posts

Posted - 01/11/2013 :  04:54:39  Show Profile  Reply with Quote
Ok, i will drive in this way
Tks anyway
carlos Lages
Brazil
Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
651 Posts

Posted - 01/11/2013 :  18:51:52  Show Profile  Reply with Quote
quote:
Originally posted by Clages1

Hi , I am using SQL2008
the sample of our friend sunitabeck
doesnt work, because it no using a XML file as Input

the main Ideia is,
pass Xml file as Parameter , Node as parameter
then read this XML file and get String between Nodes.



You first say it's not an XML file and then you say it is. Which is it? It would be better if you posted more complete code so we can tell.

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000