簡體   English   中英

如何在SQL Server中解析SOAP XML並顯示為表格

[英]How to parse SOAP XML in SQL Server and show as table

我需要在SQL Server中解析SOAP xml並將其轉換為表

<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>
    <ExecCommandResponse xmlns="http://tempuri.org/">
      <ExecCommandResult>
        <Result xmlns="">
          <row>
            <LOT>VERL5B3002PL</LOT>
            <ID>115</ID>
            <WH>710</WH>
            <STPL>12</STPL>
          </row>
          <row>
            <LOT>VERL68804EVN</LOT>
            <ID>3716</ID>
            <WH>771</WH>
            <STPL>6</STPL>
          </row>
        </Result>
      </ExecCommandResult>
    </ExecCommandResponse>
  </soap:Body>
</soap:Envelope>

我需要在SQL Server中解析SOAP xml並將其轉換為表

LOT          | ID   | WH  | STPL
VERL68804EVN | 3716 | 771 |   6

使用最新功能查詢XML。

從名稱空間看,您的XML不太干凈。 有兩個默認的名稱空間,其中一個是空的...因此,我將完全避免(屏蔽)它們。

DECLARE @xml XML=
'<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>
    <ExecCommandResponse xmlns="http://tempuri.org/">
      <ExecCommandResult>
        <Result xmlns="">
          <row>
            <LOT>VERL5B3002PL</LOT>
            <ID>115</ID>
            <WH>710</WH>
            <STPL>12</STPL>
          </row>
          <row>
            <LOT>VERL68804EVN</LOT>
            <ID>3716</ID>
            <WH>771</WH>
            <STPL>6</STPL>
          </row>
        </Result>
      </ExecCommandResult>
    </ExecCommandResponse>
  </soap:Body>
</soap:Envelope>';


SELECT r.value('LOT[1]','varchar(max)') AS LOT
      ,r.value('ID[1]','int') AS ID
      ,r.value('WH[1]','int') AS WH
      ,r.value('STPL[1]','int') AS STPL
FROM @xml.nodes('/*:Envelope/*:Body/*:ExecCommandResponse/*:ExecCommandResult/*:Result/*:row') AS A(r)

-甚至更簡單(甚至在沒有*:情況下也可以工作):

SELECT r.value('LOT[1]','varchar(max)') AS LOT
      ,r.value('ID[1]','int') AS ID
      ,r.value('WH[1]','int') AS WH
      ,r.value('STPL[1]','int') AS STPL
FROM @xml.nodes('//*:row') AS A(r)

一般來說,我會說:盡可能具體,因此建議第一個...

請嘗試以下。 看看是否可行。

declare @xmldata xml = '<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>
                                <ExecCommandResponse>
                                    <ExecCommandResult>
                                        <Result xmlns="">
                                            <row>
                                                <LOT>VERL5B3002PL</LOT>
                                                <ID>115</ID>
                                                <WH>710</WH>
                                                <STPL>12</STPL>
                                            </row>
                                            <row>
                                                <LOT>VERL68804EVN</LOT>
                                                <ID>3716</ID>
                                                <WH>771</WH>
                                                <STPL>6</STPL>
                                            </row>
                                        </Result>
                                    </ExecCommandResult>
                                </ExecCommandResponse>
                             </soap:Body>
                        </soap:Envelope>'
declare @readdoc as INT

EXEC sp_xml_preparedocument @readdoc OUTPUT, @xmldata , '<root 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" />'

select LOT,ID,WH,STPL
from OPENXML(@readdoc,'soap:Envelope/soap:Body/ExecCommandResponse/ExecCommandResult/Result/row')
with
( 
    LOT [varchar](50) 'LOT',
    ID [varchar](50) 'ID',
    WH [varchar](50) 'WH',
    STPL [varchar](50) 'STPL'
)

EXEC sp_xml_removedocument @readdoc
GO

以下是輸出。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM