Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I parse an xml string send to a SP

Posted on 2004-03-25
9
515 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

789 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