Solved

How do I parse an xml string send to a SP

Posted on 2004-03-25
9
512 Views
Last Modified: 2007-12-19
Hi Experts,

ASP receives an HTTP post with an XML string in it. ASP sends this string to a SP in SQL Server.

Now how do I instantia an XMLdocument COM object in SQL server and traverse the XML document.

Regards,

Fouad Daniels
0
Comment
Question by:FouadDaniels
  • 4
  • 4
9 Comments
 

Author Comment

by:FouadDaniels
ID: 10675749
The server is remotely hosted and the SP i make might not have all the security rights.
0
 

Author Comment

by:FouadDaniels
ID: 10675776
DECLARE @object int
DECLARE @hr int
EXEC @hr = sp_OACreate 'MSXML2.DOMDocument', @object OUT
EXEC @hr = sp_OAMethod @object, 'LoadXML', '<Root><Node></Node><Node></Node></Root>'

DECLARE @property varchar(255)
EXEC @hr = sp_OAGetProperty @object, 'nodeName', @property OUT
PRINT @property

EXEC @hr = sp_OADestroy @object

But how do I traverse the documentElement.childNodes?
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10675806
Hi FouadDaniels

>>how do I instantia an XMLdocument COM object in SQL server and traverse the XML document<<
The following code should help

I once copied it from another thread, and successfully used at my own site
I would have prefered to post a link to the initial question, but the search engine couldn't find it, and I haven't bookmarked it.

CREATE FUNCTION [dbo].[SomeFunction] (@Xml TEXT, @XPath VARCHAR(8000))  
RETURNS VARCHAR(8000)
BEGIN

     DECLARE @intNodeCount      INTEGER
     DECLARE @intDocRef      INTEGER
     DECLARE @intOut           INTEGER
     DECLARE @Return      INTEGER
     DECLARE @SelectResult      VARCHAR(8000)

     EXECUTE @intOut = sp_OACreate 'MSXML2.DOMDocument', @intDocRef OUT

     EXECUTE @intOut = sp_OAMethod @intDocRef, 'LoadXML', @Return OUT, @Xml
     EXECUTE @intOut = sp_OASetProperty @intDocRef, 'SelectionLanguage', 'XPath'
     EXECUTE @intOut = sp_OAMethod @intDocRef, 'selectSingleNode', @Return OUT, @XPath

     EXECUTE @intOut = sp_OAGetProperty @Return, 'Text', @SelectResult OUT
     EXECUTE @intOut = sp_OADestroy @intDocRef

     RETURN @SelectResult
END
go

SELECT dbo.SomeFunction('<EXAMPLE><CUSTOMER id="1" type="B">Jones</CUSTOMER><CUSTOMER id="2" type="C">Johnson</CUSTOMER></EXAMPLE>', '/EXAMPLE/CUSTOMER[. ="Jones"]')

Please be aware that I'm not the author of this code, so hopefully the one who deserves recognition for this code will read this post and participate

HTH

Hilaire
0
 

Author Comment

by:FouadDaniels
ID: 10675850
But how do I traverse the childNodes.

SPEUDO CODE:

FOR EACH NODE IN @intDocRef.documentElement.childNodes
    do something with the node
Next NODE
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Accepted Solution

by:
Hilaire earned 500 total points
ID: 10676481
This time the code's mine
It uses another method to loop through the nodes

CREATE FUNCTION [dbo].[AnotherFunction] (@Xml TEXT, @tag VARCHAR(50))  
RETURNS VARCHAR(8000)
BEGIN

     DECLARE @intNodeCount      INTEGER
     DECLARE @intDocRef      INTEGER
     DECLARE @intNodeList      INTEGER
     DECLARE @intNode      INTEGER

     DECLARE @intOut           INTEGER
     DECLARE @Return      INTEGER
     DECLARE @SelectResult      VARCHAR(8000)
     DECLARE @NodeText varchar(1000)
     declare @i int

     set @SelectResult = ''

     EXECUTE @intOut = sp_OACreate 'MSXML2.DOMDocument', @intDocRef OUT
     -- load file
     EXECUTE @intOut = sp_OAMethod @intDocRef, 'LoadXML', @Return OUT, @Xml
     -- loop through nodes of a given type (@tag parameter)
     EXECUTE @intOut = sp_OAMethod @intDocRef, 'getElementsByTagName', @intNodeList OUT, @tag
     -- get nodecount
     EXECUTE @intOut = sp_OAGetProperty @intNodeList, 'length', @intNodeCount OUT
     set @i = 0
     while @i < @intNodeCount
     begin
          -- get a pointer to each node
          EXECUTE @intOut = sp_OAMethod @intNodeList, 'nextNode', @intNode OUT
          -- get node properties
          EXECUTE @intOut = sp_OAGetProperty @intNode, 'Text', @nodetext OUT
          select @SelectResult = @SelectResult + '-' + @nodetext, @i = @i + 1
     end
     EXECUTE @intOut = sp_OADestroy @intDocRef

     RETURN @SelectResult
END

go

SELECT dbo.SomeFunction('<EXAMPLE><CUSTOMER id="1" type="B">Jones</CUSTOMER><CUSTOMER id="2" type="C">Johnson</CUSTOMER></EXAMPLE>', 'CUSTOMER')


This sample code could be used for any other purpose like loading a table,
I think you get the idea how to loop through nodes and get their properties

Hilaire
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10677059
Typo in the post above (guess you had noticed)
To test , use
SELECT dbo.AnotherFunction('<EXAMPLE><CUSTOMER id="1" type="B">Jones</CUSTOMER><CUSTOMER id="2" type="C">Johnson</CUSTOMER></EXAMPLE>', 'CUSTOMER')
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10677628
What exactly are you trying to achieve?  Have you looked at the OpenXML function?
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10704895
Hi FouadDaniels,
did my code help a bit ?

I didn't mention OPENXML because It won't be able to parse complicated XML trees, or simple XML trees with recursive structure ( eg when one <Element> tag can include one or more <Element> tags)

But, as Anthony suggested, it's definitely better than creating activeX objects if the file structure allows its usage.

Cheers

Hilaire
0
 

Author Comment

by:FouadDaniels
ID: 10704969
I am not at work now. tomorow all come back on this. sorry for the delay.

Regards,
Fouad Daniels
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now