【问题标题】:SQL Server 2008 Querying Soap XMLSQL Server 2008 查询 Soap XML
【发布时间】:2012-06-07 04:02:47
【问题描述】:

我一直在尝试使用 T-SQL 处理这个 SOAP XML 返回,但我得到的只是 NULL 或什么都没有。我尝试了不同的方法并将它们全部粘贴在下面。

Declare @xmlMsg xml;

Set @xmlMsg = 
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <SendWarrantyEmailResponse xmlns="http://Web.Services.Warranty/">
      <SendWarrantyEmailResult xmlns="http://Web.Services.SendWarrantyResult">
        <WarrantyNumber>120405000000015</WarrantyNumber>
        <Result>Cannot Send Email to anonymous account!</Result>
        <HasError>true</HasError>
        <MsgUtcTime>2012-06-07T01:11:36.8665126Z</MsgUtcTime>
      </SendWarrantyEmailResult>
    </SendWarrantyEmailResponse>
  </soap:Body>
</soap:Envelope>';

declare @table table (data xml);
insert into @table values (@xmlMsg);

select data from @table;

WITH xmlnamespaces ('http://schemas.xmlsoap.org/soap/envelope/' as [soap], 'http://Web.Services.Warranty' as SendWarrantyEmailResponse, 'http://Web.Services.SendWarrantyResult' as SendWarrantyEmailResult)
SELECT Data.value('(/soap:Envelope[1]/soap:Body[1]/SendWarrantyEmailResponse[1]/SendWarrantyEmailResult[1]/WarrantyNumber[1])[1]','VARCHAR(500)') AS WarrantyNumber
FROM @Table ;

;with xmlnamespaces('http://schemas.xmlsoap.org/soap/envelope/' as [soap],'http://Web.Services.Warranty' as SendWarrantyEmailResponse,'http://Web.Services.SendWarrantyResult' as SendWarrantyEmailResult)
--select @xmlMsg.value('(/soap:Envelope/soap:Body/SendWarrantyEmailResponse/SendWarrantyEmailResult/WarrantyNumber)[0]', 'nvarchar(max)')
--select T.N.value('.', 'nvarchar(max)') from @xmlMsg.nodes('/soap:Envelope/soap:Body/SendWarrantyEmailResponse/SendWarrantyEmailResult') as T(N)
select @xmlMsg.value('(/soap:Envelope/soap:Body/SendWarrantyEmailResponse/SendWarrantyEmailResult/HasError)[1]','bit') as test

;with xmlnamespaces('http://schemas.xmlsoap.org/soap/envelope/' as [soap],'http://Web.Services.Warranty' as [SendWarrantyEmailResponse],'http://Web.Services.SendWarrantyResult' as [SendWarrantyEmailResult])
SELECT
 SendWarrantyEmailResult.value('WarrantyNumber[1]','varchar(max)') AS WarrantyNumber,
 SendWarrantyEmailResult.value('Result[1]','varchar(max)') AS Result,
 SendWarrantyEmailResult.value('HasError[1]','bit') AS HasError,
 SendWarrantyEmailResult.value('MsgUtcTime[1]','datetime') AS MsgUtcTime
FROM @xmlMsg.nodes('/soap:Envelope/soap:Body/SendWarrantyEmailResponse/SendWarrantyEmailResult') SendWarrantyEmailResults(SendWarrantyEmailResult)

【问题讨论】:

    标签: xml sql-server-2008 tsql soap


    【解决方案1】:

    嗯,我有时间重新考虑,我想出了答案,所以我相信有人会在找这个。

    仔细查看命名空间的缩写,它的神奇之处在于它使一切正常工作。希望这可以帮助那里的人

    Declare @xmlMsg xml;
    
    Set @xmlMsg = 
    '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
        <SendWarrantyEmailResponse xmlns="http://Web.Services.Warranty/">
          <SendWarrantyEmailResult xmlns="http://Web.Services.SendWarrantyResult">
            <WarrantyNumber>120405000000015</WarrantyNumber>
            <Result>Cannot Send Email to anonymous account!</Result>
            <HasError>true</HasError>
            <MsgUtcTime>2012-06-07T01:11:36.8665126Z</MsgUtcTime>
          </SendWarrantyEmailResult>
        </SendWarrantyEmailResponse>
      </soap:Body>
    </soap:Envelope>';
    
    declare @table table (data xml);
    insert into @table values (@xmlMsg);
    
    select data from @table;
    
    WITH xmlnamespaces (
    'http://schemas.xmlsoap.org/soap/envelope/' as [soap], 
    'http://Web.Services.Warranty/' as Resp, 
    'http://Web.Services.SendWarrantyResult' as Res)
    SELECT Data.value('(/soap:Envelope/soap:Body/Resp:SendWarrantyEmailResponse/Res:SendWarrantyEmailResult/Res:WarrantyNumber)[1]','VARCHAR(500)') AS WarrantyNumber
    FROM @Table ;
    
    ;with xmlnamespaces(
    'http://schemas.xmlsoap.org/soap/envelope/' as [soap],
    'http://Web.Services.Warranty/' as Resp,
    'http://Web.Services.SendWarrantyResult' as Res)
    select @xmlMsg.value('(/soap:Envelope/soap:Body/Resp:SendWarrantyEmailResponse/Res:SendWarrantyEmailResult/Res:WarrantyNumber)[1]', 'nvarchar(max)')
    --select T.N.value('.', 'nvarchar(max)') from @xmlMsg.nodes('/soap:Envelope/soap:Body/SendWarrantyEmailResponse/SendWarrantyEmailResult:SendWarrantyEmailResult') as T(N)
    --select @xmlMsg.value('(/soap:Envelope/soap:Body/SendWarrantyEmailResponse/SendWarrantyEmailResult/SendWarrantyEmailResult:HasError)[1]','bit') as test
    
    ;with xmlnamespaces(
    'http://schemas.xmlsoap.org/soap/envelope/' as [soap],
    'http://Web.Services.Warranty/' as Resp,
    'http://Web.Services.SendWarrantyResult' as Res)
    SELECT
     SendWarrantyEmailResult.value('Res:WarrantyNumber[1]','varchar(max)') AS WarrantyNumber,
     SendWarrantyEmailResult.value('Res:Result[1]','varchar(max)') AS Result,
     SendWarrantyEmailResult.value('Res:HasError[1]','bit') AS HasError,
     SendWarrantyEmailResult.value('Res:MsgUtcTime[1]','datetime') AS MsgUtcTime
    FROM @xmlMsg.nodes('/soap:Envelope/soap:Body/Resp:SendWarrantyEmailResponse/Res:SendWarrantyEmailResult') SendWarrantyEmailResults(SendWarrantyEmailResult)
    

    【讨论】:

    • 感谢您回来并发布您的答案。它帮助我解决了我们遇到的一个问题,并且当人们做出贡献时,即使他们无法获得帮助,它也会使整个网站变得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多