使用SQL Server连接xml接口,读取并解析数据

 

--数据源格式,放到任意程序中部署接口即可
--
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> --<Peoples> --<People> --<Name>张三</Name> --<Sex>男</Sex> --</People> --<People> --<Name>李四</Name> --<Sex>女</Sex> --</People> --<People> --<Name>王武</Name> --<Sex>男</Sex> --</People> --<People> --<Name>赵柳</Name> --<Sex>女</Sex> --</People> --<People> --<Name>武士刀</Name> --<Sex>男</Sex> --</People> --</Peoples> --</Data> --调用webService---------------- declare @ServiceUrl as varchar(1000) DECLARE @UrlAddress varchar(500) --WebService地址:以http开头,结尾带斜杠,例如'https://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/' set @UrlAddress = 'http://localhost:11687/home/webxml' SET @ServiceUrl=@UrlAddress--如果有参数可以在此处拼入 --访问地址获取结果 Declare @Object as Int Declare @ResponseText as Varchar(8000) --必须8000 Declare @Data as XML EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; --创建OLE组件对象 Exec sp_OAMethod @Object, 'open', NULL, 'POST',@ServiceUrl,'false' --打开链接,注意是get还是post Exec sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT --输出参数 Select @ResponseText --输出结果 SET @Data = CAST(@ResponseText AS XML) select t.c.value('(Name/text())[1]','VARCHAR(20)') as Name, t.c.value('(Sex/text())[1]','VARCHAR(20)') as Sex from @Data.nodes('/*/*/*') as t(c) Exec sp_OADestroy @Object GO ----开启 Ole Automation Procedures --sp_configure 'show advanced options', 1; --GO --RECONFIGURE; --GO --sp_configure 'Ole Automation Procedures', 1; --GO --RECONFIGURE; --GO --EXEC sp_configure 'Ole Automation Procedures'; --GO ----关闭 Ole Automation Procedures --sp_configure 'show advanced options', 0; --GO --RECONFIGURE; --GO --sp_configure 'Ole Automation Procedures', 0; --GO --RECONFIGURE; --GO --EXEC sp_configure 'Ole Automation Procedures'; --GO
----开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句:
--exec sp_configure 'show advanced options',1
--reconfigure
--exec sp_configure 'Ad Hoc Distributed Queries',1
--reconfigure
----关闭Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句:
--exec sp_configure 'Ad Hoc Distributed Queries',0
--reconfigure
--exec sp_configure 'show advanced options',0
--reconfigure

 

 

 

posted @ 2018-08-01 14:46  听雨的人  阅读(1561)  评论(0编辑  收藏  举报