2014-01-16 71 views
0

如何將數據從xml保存到sql 2008?如何將數據從xml保存到sql 2008?

SQL表:

[dbo].[Position](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [ImoNo] [numeric](8, 0) NOT NULL, 
    [sid] [numeric](5, 0) NULL, 
    [VesselName] [nvarchar](20) NULL, 
    [time] [datetime] NOT NULL, 
    [lat] [numeric](9, 2) NULL, 
    [lon] [numeric](9, 2) NULL, 
    [sog] [numeric](9, 2) NULL, 
    [cog] [numeric](9, 2) NULL, 
     [hdg] [numeric](9, 2) NULL, 
    [eta] [datetime] NULL, 
    [NextPort] [nvarchar](20) NULL) 

XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ns1="http://www.fleettracker.de/api/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<SOAP-ENV:Body> 
<ns1:GetPositionsResponse> 
    <body> 
    <result>Found 2 vessels.</result> 
    <success>true</success> 
    <shipsWithPositions xsi:type="ns1:FleettrackerShip"> 
     <ns1:imono>9456159</ns1:imono> 
     <ns1:sid>780</ns1:sid> 
     <ns1:name>Trenta</ns1:name> 
     <ns1:charterShipName>Trenta</ns1:charterShipName> 
     <ns1:pasttrack> 
     <lat>1832900</lat> 
     <lon>7570400</lon> 
     <timestamp>2014-01-14T08:28:45Z</timestamp> 
     <orderNumber>0</orderNumber> 
     <sog>9.5</sog> 
     <cog>22</cog> 
     <hdg>22</hdg> 
     <eta>2014-01-15T12:00:00</eta> 
     <nextport>KWANGYANG</nextport> 
     </ns1:pasttrack> 
     <ns1:pasttrack> 
     <lat>1872560</lat> 
     <lon>7589000</lon> 
     <timestamp>2014-01-14T07:00:00Z</timestamp> 
     <orderNumber>1</orderNumber> 
     <sog>10.8</sog> 
     <cog>25</cog> 
     <hdg>25</hdg> 
     </ns1:pasttrack> 
    </shipsWithPositions> 
<shipsWithPositions xsi:type="ns1:FleettrackerShip"> 
     <ns1:imono>9144055</ns1:imono> 
     <ns1:sid>789</ns1:sid> 
     <ns1:name>Vipava</ns1:name> 
     <ns1:charterShipName>Vipava</ns1:charterShipName> 
     <ns1:pasttrack> 
     <lat>1757160</lat> 
     <lon>7536240</lon> 
     <timestamp>2014-01-13T19:00:00Z</timestamp> 
     <orderNumber>2</orderNumber> 
     <sog>9.4</sog> 
     <cog>21</cog> 
     <hdg>21</hdg> 
     </ns1:pasttrack> 
     <ns1:pasttrack> 
     <lat>1658200</lat> 
     <lon>7476480</lon> 
     <timestamp>2014-01-13T07:00:00Z</timestamp> 
     <orderNumber>3</orderNumber> 
     <sog>8.4</sog> 
     <cog>29</cog> 
     <hdg>29</hdg> 
     </ns1:pasttrack> 
     <ns1:pasttrack> 
     <lat>1630000</lat> 
     <lon>7455400</lon> 
     <timestamp>2014-01-13T03:00:03Z</timestamp> 
     <orderNumber>4</orderNumber> 
     <sog>8.83</sog> 
     <cog>34</cog> 
     <hdg>34</hdg> 
     <eta>2014-01-15T08:00:00</eta> 
     <nextport>KWANGYANG</nextport> 
     </ns1:pasttrack> 
    </shipsWithPositions> 
    </body> 
</ns1:GetPositionsResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我試圖與此查詢,但我得到錯誤味精

'XQuery [nodes()]: The names "SOAP-ENV" and "ns1:" do not denote a namespace.' 
DECLARE @xml XML 
DECLARE @character VARCHAR(MAX) 

SELECT @character = x.y 
FROM OPENROWSET(BULK 'C:\Users\Nale\Desktop\POS.xml', SINGLE_CLOB) x(y) 

-- Fix up the ampersand 
SELECT @xml = REPLACE(@character, '&', '&amp;') 


-- Get the tally information 
SELECT 
x.y.value('ns1:imono/text())[1]', 'NUMERIC (8,0)') ImoNo, 
x.y.value('ns1:sid/text())[1]', 'NUMERIC (5,0)') sid, 
x.y.value('ns1:VesselName/text())[1]', 'NVARCHAR (20)') VesselName, 
x.y.value('ns1:pasttrack/time/text())[1]', 'DATETIME') time, 
x.y.value('ns1:pasttrack/lat/text())[1]', 'NUMERIC (9,2)') lat, 
x.y.value('ns1:pasttrack/lon/text())[1]', 'NUMERIC (9,2)') lon, 
x.y.value('ns1:pasttrack/sog/text())[1]', 'NUMERIC (9,2)') sog, 
x.y.value('ns1:pasttrack/cog/text())[1]', 'NUMERIC (9,2)') cog, 
x.y.value('ns1:pasttrack/hdg/text())[1]', 'NUMERIC (9,2)') hdg, 
x.y.value('ns1:pasttrack/eta/text())[1]', 'DATETIME') eta, 
x.y.value('ns1:pasttrack/NextPort/text())[1]', 'NVARCHAR (20)') NextPort 
FROM @xml.nodes('SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:GetPositionsResponse/body/shipsWithPositions') AS x(y) 

XML文件在本地磁盤。

我會使用一些sql查詢,或者什麼是將數據從xml保存到sql表的最好方法?

+0

任何幫助嗎? – Nale

回答

1
  1. 使XML有效
<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ns1="http://www.fleettracker.de/api/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <SOAP-ENV:Body> 
     <ns1:GetPositionsResponse> 
      <body> 
       <result>Found 2 vessels.</result> 
       <success>true</success> 
       <shipsWithPositions xsi:type="ns1:FleettrackerShip"> 
        <ns1:imono>9456159</ns1:imono> 
        <ns1:sid>780</ns1:sid> 
        <ns1:name>Trenta</ns1:name> 
        <ns1:charterShipName>Trenta</ns1:charterShipName> 
        <ns1:pasttrack> 
         <lat>1832900</lat> 
         <lon>7570400</lon> 
         <timestamp>2014-01-14T08:28:45Z</timestamp> 
         <orderNumber>0</orderNumber> 
         <sog>9.5</sog> 
         <cog>22</cog> 
         <hdg>22</hdg> 
         <eta>2014-01-15T12:00:00</eta> 
         <nextport>KWANGYANG</nextport> 
        </ns1:pasttrack> 
        <ns1:pasttrack> 
         <lat>1872560</lat> 
         <lon>7589000</lon> 
         <timestamp>2014-01-14T07:00:00Z</timestamp> 
         <orderNumber>1</orderNumber> 
         <sog>10.8</sog> 
         <cog>25</cog> 
         <hdg>25</hdg> 
        </ns1:pasttrack> 
       </shipsWithPositions> 
       <shipsWithPositions xsi:type="ns1:FleettrackerShip"> 
        <ns1:imono>9144055</ns1:imono> 
        <ns1:sid>789</ns1:sid> 
        <ns1:name>Vipava</ns1:name> 
        <ns1:charterShipName>Vipava</ns1:charterShipName> 
        <ns1:pasttrack> 
         <lat>1757160</lat> 
         <lon>7536240</lon> 
         <timestamp>2014-01-13T19:00:00Z</timestamp> 
         <orderNumber>2</orderNumber> 
         <sog>9.4</sog> 
         <cog>21</cog> 
         <hdg>21</hdg> 
        </ns1:pasttrack> 
        <ns1:pasttrack> 
         <lat>1658200</lat> 
         <lon>7476480</lon> 
         <timestamp>2014-01-13T07:00:00Z</timestamp> 
         <orderNumber>3</orderNumber> 
         <sog>8.4</sog> 
         <cog>29</cog> 
         <hdg>29</hdg> 
        </ns1:pasttrack> 
        <ns1:pasttrack> 
         <lat>1630000</lat> 
         <lon>7455400</lon> 
         <timestamp>2014-01-13T03:00:03Z</timestamp> 
         <orderNumber>4</orderNumber> 
         <sog>8.83</sog> 
         <cog>34</cog> 
         <hdg>34</hdg> 
         <eta>2014-01-15T08:00:00</eta> 
         <nextport>KWANGYANG</nextport> 
        </ns1:pasttrack> 
       </shipsWithPositions> 
      </body> 
     </ns1:GetPositionsResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

2.添加命名空間查詢

DECLARE @xml XML 
DECLARE @character VARCHAR(MAX) 

SELECT @xml = x.y 
FROM OPENROWSET(BULK 'C:\Users\Nale\Desktop\POS.xml', SINGLE_CLOB) x(y) 


-- Get the tally information 
;WITH XMLNAMESPACES (
    'http://www.fleettracker.de/api/1.0' as ns1, 
    'http://www.w3.org/2001/XMLSchema-instance' AS xsi, 
    'http://schemas.xmlsoap.org/soap/envelope/' AS e 
) 
SELECT 
    x.y.value('(ns1:imono/text())[1]', 'NUMERIC (8,0)') ImoNo, 
    x.y.value('(ns1:sid/text())[1]', 'NUMERIC (5,0)') sid, 
    x.y.value('(ns1:VesselName/text())[1]', 'NVARCHAR (20)') VesselName, 
    x.y.value('(ns1:pasttrack/time/text())[1]', 'DATETIME') time, 
    x.y.value('(ns1:pasttrack/lat/text())[1]', 'NUMERIC (9,2)') lat, 
    x.y.value('(ns1:pasttrack/lon/text())[1]', 'NUMERIC (9,2)') lon, 
    x.y.value('(ns1:pasttrack/sog/text())[1]', 'NUMERIC (9,2)') sog, 
    x.y.value('(ns1:pasttrack/cog/text())[1]', 'NUMERIC (9,2)') cog, 
    x.y.value('(ns1:pasttrack/hdg/text())[1]', 'NUMERIC (9,2)') hdg, 
    x.y.value('(ns1:pasttrack/eta/text())[1]', 'DATETIME') eta, 
    x.y.value('(ns1:pasttrack/NextPort/text())[1]', 'NVARCHAR (20)') NextPort 
FROM @xml.nodes('e:Envelope/e:Body/ns1:GetPositionsResponse/body/shipsWithPositions') AS x(y) 
相關問題