parsing xml using tsql and http response


I'd like to parse some xml from a URL but without having to create a temporary table i.e. #xml, is this possible, I've tried with code below, which runs ok but doesn't return any values.

Thank you

--IF OBJECT_ID('tempdb..#xml') IS NOT NULL DROP TABLE #xml
--CREATE TABLE #xml ( yourXML XML )


DECLARE @QS varchar(50)

-- & or ? depending if there are other query strings
-- Use this for when there is other query strings:
SELECT @QS = '&date='+convert(varchar(25),getdate(),126)
-- Use this for when there is NO other query strings:
-- SELECT @QS = '?date='+convert(varchar(25),getdate(),126)
SELECT @URL = ',-65.598409&sensor=false'  + @QS

DECLARE @Response varchar(8000)
DECLARE @Obj int 
DECLARE @Result int 
DECLARE @HTTPStatus int 
DECLARE @ErrorMsg varchar(MAX)

EXEC @Result = sp_OACreate 'MSXML2.XMLHttp', @Obj OUT 

EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'GET', @URL, false
EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded'
EXEC @Result = sp_OAMethod @Obj, send, NULL, ''
EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT 
EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml', @Response OUT

SET @XML = @Response

SELECT  T.A.value('status[1]','VARCHAR(MAX)')
FROM @XML.nodes('GeocodeResponse/status') AS T(A)

Open in new window

crompnkData Management SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
You're already on the status element. Thus your XQuery in the values() is incorrect:


SET @XML = N'<GeocodeResponse><status>OK</status><result><type>route</type><formatted_address>Carretera Cocomar, Venezuela</formatted_address><address_component><long_name>Carretera Cocomar</long_name><short_name>Carr Cocomar</short_name><type>route</type></address_component><address_component><long_name>Pedro Gual</long_name><short_name>Pedro Gual</short_name><type>administrative_area_level_2</type><type>political</type></address_component><address_component><long_name>Miranda</long_name><short_name>Miranda</short_name><type>administrative_area_level_1</type><type>political</type></address_component><address_component><long_name>Venezuela</long_name><short_name>VE</short_name><type>country</type><type>political</type></address_component><geometry><location><lat>10.1757820</lat><lng>-65.5769751</lng></location><location_type>GEOMETRIC_CENTER</location_type><viewport><southwest><lat>10.1719597</lat><lng>-65.5796895</lng></southwest><northeast><lat>10.1805279</lat><lng>-65.5748096</lng></northeast></viewport><bounds><southwest><lat>10.1719597</lat><lng>-65.5796895</lng></southwest><northeast><lat>10.1805279</lat><lng>-65.5748096</lng></northeast></bounds></geometry><place_id>ChIJGbTDIfqRLIwRpcUexV7sUZI</place_id></result><result><type>country</type><type>political</type><formatted_address>Venezuela</formatted_address><address_component><long_name>Venezuela</long_name><short_name>VE</short_name><type>country</type><type>political</type></address_component><geometry><location><lat>6.4237500</lat><lng>-66.5897300</lng></location><location_type>APPROXIMATE</location_type><viewport><southwest><lat>0.5438179</lat><lng>-73.4255611</lng></southwest><northeast><lat>12.7704090</lat><lng>-59.8037800</lng></northeast></viewport><bounds><southwest><lat>0.6475290</lat><lng>-73.3515580</lng></southwest><northeast><lat>12.4866941</lat><lng>-59.8056660</lng></northeast></bounds></geometry><place_id>ChIJAdjLNstTKIwR003VfFjyoNw</place_id></result></GeocodeResponse>';

SELECT  [status].value('.', 'VARCHAR(MAX)')
FROM    @XML.nodes('GeocodeResponse/status') AS A ( [status] );

SELECT  GeocodeResponse.value('status[1]', 'VARCHAR(MAX)')
FROM    @XML.nodes('GeocodeResponse') AS A ( GeocodeResponse );

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.