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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM