简体   繁体   中英

Extracting information from XML in SQL Server

I need to extract the information from XML in SQL Server. The format of the file is similar the one below. I'm having trouble parsing out the info from this style of document. Does anyone know of a why to accomplish this?

<ExtractSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConnectorSettings connectorScriptId="74" channelDomain="Diag" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
    <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="15" channelDomain="Doc" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
      <StepSettings stepScriptId="61" stepType="Lab" isActive="true" sequence="3" />
   </ConnectorSettings>
  <ConnectorSettings connectorScriptId="12" channelDomain="Imm" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="16" stepType="Imm" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="46" channelDomain="Lab" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
  </ConnectorSettings>
</ExtractSettings>

I've done this before in SQL Server, but the format was something like this

  <ConnectorSetting>
     <StepSettings>70<StepSettings/>
        <Steptype>Enc<Steptype/>
     <StepSettings>23<StepSettings/>
        <Steptype>Demo<Steptype/>
  </ConnectorSettings>

which made It much easier to process.

I've tried a couple of things but have been mostly trying to get these to work

 select 
     stuff.connectorSettings.value('stepScriptID', 'varchar(50)') 
 from 
     @xml.nodes('ROOT/ConnectorSettings/stepscript') as  stuff(connectorSettings);

and

exec sp_xml_preparedocument @idoc output, @xml;
select * from OPENXML(@idoc,'/ROOT/ConnectorSettings',4)

I'm kinda lost at this point So any advice would be appreciated.

Here's a little sample to get you started:

declare @doc xml = '<ExtractSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConnectorSettings connectorScriptId="74" channelDomain="Diag" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
    <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="15" channelDomain="Doc" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
      <StepSettings stepScriptId="61" stepType="Lab" isActive="true" sequence="3" />
   </ConnectorSettings>
  <ConnectorSettings connectorScriptId="12" channelDomain="Imm" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="16" stepType="Imm" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="46" channelDomain="Lab" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
  </ConnectorSettings>
</ExtractSettings>';

select cs.value('@connectorScriptId','int') connectorScriptId, 
       ss.value('@stepType','varchar(20)') stepType
from @doc.nodes('/ExtractSettings/ConnectorSettings') c(cs)
outer apply cs.nodes('StepSettings') cs(ss)

outputs

connectorScriptId stepType
----------------- --------------------
74                Demo
74                Enc
15                Demo
15                Enc
15                Lab
12                Demo
12                Imm
46                Demo

(8 rows affected)

Given your XML in a XML variable @xml , you could try something like this:

SELECT
    ConnectorScriptId = xc.value('../@connectorScriptId', 'int'),
    StepScriptId = xc.value('@stepScriptId', 'int'),
    StepType = xc.value('@stepType', 'varchar(50)'),
    Sequence = xc.value('@sequence', 'int')
FROM
    @xml.nodes('/ExtractSettings/ConnectorSettings/StepSettings') AS XT(XC)

That should give you something like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM