如何在SQL Server中解析SOAP XML并显示为表

时间:2016-08-11 20:01:23

标签: sql sql-server xml web-services soap

我需要在SQL Server中解析SOAP xml并将其转换为表

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

我需要在sql server中解析SOAP xml并将其转换为表

@Transactional
public int getUserIdCreateIfNotExistent(String email) throws Exception {
  try {
    return jdbcTemplate.queryForObject("SELECT id FROM users WHERE email = ?", Integer.class, email);
  } catch (IncorrectResultSizeDataAccessException e) {
    Thread.sleep(10000);
    return jdbcTemplate.queryForObject("INSERT INTO users (email) values(?) RETURNING id", Integer.class, email);
  }
}

2 个答案:

答案 0 :(得分:1)

使用最新功能查询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)

总的来说,我会说:尽可能具体,因此建议第一个......

答案 1 :(得分:0)

尝试下面。看看它是否有效。

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

以下是输出。

enter image description here