Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Reading XML File into a table


Reading XML File into a table

Author
Message
nadersam
nadersam
SSC-Enthusiastic
SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)

Group: General Forum Members
Points: 196 Visits: 431
Hi All,
When i try to read the attached xml file using the following commands

DECLARE @XMLDocPointer INT
EXEC sp_xml_preparedocument @XMLDocPointer OUTPUT, 'C:\Export1\med_Form.xml'



SELECT * FROM OPENXML(@XMLDocPointer,'/med_Form/medFormID',2 )

I get error

The XML parse error 0xc00ce556 occurred on line number 1, near the XML text "C:\Export1\Alchemy_Form.xml".
Msg 6602, Level 16, State 2, Procedure sp_xml_preparedocument, Line 1
The error description is 'Invalid at the top level of the document.'.
Can someone help please with that, i need to read all contents of that file into a database table.

Regards
Nader
Attachments
Med_Form.txt (36 views, 31.00 KB)
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
What SQL Server version do you use?
It might be easier to do it using XQuery, assuming you use SQL 2005 and up:
SELECT 
v.value('medFormID[1]','int') AS ID,
v.value('FormName[1]','varchar(50)') AS FormName
FROM @xml.nodes('document') T(c)
CROSS APPLY T.c.nodes('med_Form')U(v)





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
nadersam
nadersam
SSC-Enthusiastic
SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)

Group: General Forum Members
Points: 196 Visits: 431
Thanks very much for your reply.
I am using SQL 2008 , using your technique i must first read the xml file, should i do that using the sp_xml_preparedocument stored procedure ?.
Because it seems it's the one causing the previous error.

Thanks again
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
You could use
SELECT * FROM OPENROWSET(
BULK 'c:\SampleFolder\SampleData3.txt',
SINGLE_BLOB) AS x





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
paul_ramster
paul_ramster
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 534
sp_xml_preparedocument doesn't read a file from disk, it creates a handle to an internal XML document (variable). To read the attached XML file into a variable, see this link, or (rather more simply), follow LutzM's advice and use OPENROWSET().

DECLARE @XML xml
SELECT @xml = x.a
FROM OPENROWSET(BULK 'C:\Med_Form.xml', SINGLE_BLOB) AS x(a)

--either query with OPENXML()
DECLARE @XMLDocPointer INT
EXEC sp_xml_preparedocument @XMLDocPointer OUTPUT, @xml
SELECT * FROM OPENXML(@XMLDocPointer,N'/document/med_Form/medFormID', 0)

-- or with XQuery
SELECT doc.medFormID.value('.[1]','int') AS ID
, doc.medFormID.value('../FormName[1]','varchar(60)') AS FormName
FROM @xml.nodes('document/med_Form/medFormID') doc(medFormID)


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