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

白昼怎懂夜的黑 提交于 2019-12-25 04:45:29

问题


I need to parser a SOAP xml in SQL Server and convert it to table

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

I need to parser a SOAP xml in sql server and convert it to table

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

回答1:


Use the up-to-date functions to query XML.

Your XML is not very clean looking on the namespaces. There are two default namespaces, one of them empty... Therefore I would avoid (mask) them entirely.

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)

--or even simpler (would even work without the *:):

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)

In general I'd say: Be as specific as possible, therefore rather suggest the first...




回答2:


Try Below. See if it works.

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

Below is the output.



来源:https://stackoverflow.com/questions/38905120/how-to-parse-soap-xml-in-sql-server-and-show-as-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!