| Author |
Topic  |
|
|
Clages1
Yak Posting Veteran
65 Posts |
Posted - 01/09/2013 : 07:11:49
|
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
5152 Posts |
Posted - 01/09/2013 : 07:41:51
|
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.
|
 |
|
|
Jeff Moden
Aged Yak Warrior
USA
644 Posts |
Posted - 01/09/2013 : 19:50:47
|
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. - Are you saying that you have a file on the disk that you'd first like to import into SQL Server?
- 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 |
 |
|
|
Clages1
Yak Posting Veteran
65 Posts |
Posted - 01/10/2013 : 12:36:44
|
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
|
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 01/10/2013 : 15:56:43
|
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.
|
 |
|
|
Clages1
Yak Posting Veteran
65 Posts |
Posted - 01/11/2013 : 04:54:39
|
Ok, i will drive in this way Tks anyway carlos Lages Brazil |
 |
|
|
Jeff Moden
Aged Yak Warrior
USA
644 Posts |
Posted - 01/11/2013 : 18:51:52
|
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." |
 |
|
| |
Topic  |
|