MSSQL 2008R2中的XML解析错误

时间:2014-01-15 11:00:15

标签: sql-server xml soap

这是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 1 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>1224000</lat>
            <lon>7188500</lon>
            <timestamp>2014-01-11T08:07:45Z</timestamp>
            <orderNumber>9</orderNumber>
            <sog>7</sog>
            <cog>39</cog>
            <hdg>39</hdg>
            <eta>2014-01-15T08:00:00</eta>
            <nextport>KWANGYANG</nextport>
          </ns1:pasttrack>
                </shipsWithPositions>
            </body>
        </ns1:GetPositionsResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是MsSql查询:

EXEC sp_xml_preparedocument @idoc OUTPUT, @XML,'<root xmlns:ns1="http://www.fleettracker.de/api/1.0">' 
--select @idoc 

insert into Position (ImoNo, sid, VesselName, time, lat, lon, sog, cog, hdg, eta, NextPort)

SELECT    *
FROM       OPENXML (@idoc, '/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:GetPositionsResponse/body/shipsWithPositions', 2)
WITH    (ImoNo numeric(8, 0) 'ns1:imono',                                                    
        sid numeric(5, 0) 'ns1:sid',  
        VesselName nvarchar(20) 'ns1:name',  
        time datetime 'ns1:pasttrack/timestamp',  
        lat numeric(9, 2) 'ns1:pasttrack/lat',  
        lon numeric(9, 2) 'ns1:pasttrack/lon',
        sog numeric(9, 2) 'ns1:pasttrack/sog',  
        cog numeric(9, 2) 'ns1:pasttrack/cog',  
        hdg numeric(9, 2) 'ns1:pasttrack/hdg',  
        eta datetime 'ns1:pasttrack/eta',  
        NextPort nvarchar(20) 'ns1:pasttrack/nextport'

        );

EXEC sp_xml_removedocument @idoc

有人知道我为什么会得到XML解析错误吗?!

----------------------------------------------- --------------

----------------------------------------------- --------------


1 个答案:

答案 0 :(得分:0)

问题是@XML变量的类型。

由于文件标题为decalres&#39; UTF-8&#39;编码时,文档必须位于varchar()变量中:

declare @XML varchar(max) = 'your UTF-8 document'

如果您将其转换为nvarchar(max)&#39; UTF-8&#39;编码无效。

declare @XML nvarchar(max) = 'your UTF-8 document' -- This cannot be parsed!