将Xml读入ms sql

时间:2016-10-06 19:41:46

标签: sql-server xml xml-parsing

我想将下面粘贴的xml读入sql。我尝试了几种从Web上解析这个xml的方法而没有成功。 我尝试了以下脚本:

CREATE TABLE [dbo].[Ejemplo2](
[RutEmisor][nvarchar](12) null,
[RutEnvia][nvarchar](12) null,
[RutReceptor][nvarchar](12) null,
[FchResol][nvarchar](12) null,
[NroResol][nvarchar](12) null
)
GO

DECLARE @messagebody XML

SELECT @messagebody = BulkColumn
FROM OPENROWSET(BULK 'C:\Ejemplo.xml', SINGLE_CLOB) AS X

INSERT INTO [dbo].[Ejemplo2]
select  a.value(N'(./RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
    a.value(N'(./RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
    a.value(N'(./RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
    a.value(N'(./FchResol)[1]', N'nvarchar(12)') as [FchResol],
    a.value(N'(./NroResol)[1]', N'nvarchar(12)') as [NroResol]
from @messagebody.nodes('/EnvioDTE/SetDTE/Caratula') as r(a);


Select * from dbo.Ejemplo2

但我得到0行 我尝试了更简单的例子,并总是返回数据,但在这种情况下,我无法想象什么是错的。 任何有关这方面的帮助将不胜感激。

xml如下

<?xml version="1.0" encoding="ISO-8859-1"?>
<EnvioDTE xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
<SetDTE ID="SetDoc">
  <TmstFirmaEnv>2003-10-13T09:33:22</TmstFirmaEnv>
 <Caratula version="1.0">
  <RutEmisor>97975000-5</RutEmisor>
  <RutEnvia>7880442-4</RutEnvia>
  <RutReceptor>60803000-K</RutReceptor>
  <FchResol>2003-09-02</FchResol>
  <NroResol>0</NroResol>
  <SubTotDTE>
   <TpoDTE>33</TpoDTE>
   <NroDTE>1</NroDTE>
  </SubTotDTE>
 </Caratula>
<DTE version="1.0">
    <Documento ID="F60T33">
        <Encabezado>
            <IdDoc>
                <TipoDTE>33</TipoDTE>
                <Folio>60</Folio>
                <FchEmis>2003-10-13</FchEmis>
            </IdDoc>
            <Emisor>
                <RUTEmisor>97975000-5</RUTEmisor>
                <RznSoc>RUT DE PRUEBA</RznSoc>
                <GiroEmis>Insumos de Computacion</GiroEmis>
                <Acteco>31341</Acteco>
                <CdgSIISucur>1234</CdgSIISucur>
                <DirOrigen>Teatinos 120, Piso 4</DirOrigen>
                <CmnaOrigen>Santiago</CmnaOrigen>
                <CiudadOrigen>Santiago</CiudadOrigen>
            </Emisor>
            <Receptor>
                <RUTRecep>77777777-7</RUTRecep>
                <RznSocRecep>EMPRESA  LTDA</RznSocRecep>
                <GiroRecep>COMPUTACION</GiroRecep>
                <DirRecep>SAN DIEGO 2222</DirRecep>
                <CmnaRecep>LA FLORIDA</CmnaRecep>
                <CiudadRecep>SANTIAGO</CiudadRecep>
            </Receptor>
            <Totales>
                <MntNeto>100000</MntNeto>
                <TasaIVA>19</TasaIVA>
                <IVA>19000</IVA>
                <MntTotal>119000</MntTotal>
            </Totales>
        </Encabezado>
        <Detalle>
            <NroLinDet>1</NroLinDet>
            <CdgItem>
                <TpoCodigo>INT1</TpoCodigo>
                <VlrCodigo>011</VlrCodigo>
            </CdgItem>
            <NmbItem>Parlantes Multimedia 180W.</NmbItem>
            <DscItem/>
            <QtyItem>20</QtyItem>
            <PrcItem>4500</PrcItem>
            <MontoItem>90000</MontoItem>
        </Detalle>
        <Detalle>
            <NroLinDet>2</NroLinDet>
            <CdgItem>
                <TpoCodigo>INT1</TpoCodigo>
                <VlrCodigo>0231</VlrCodigo>
            </CdgItem>
            <NmbItem>Mouse Inalambrico PS/2</NmbItem>
            <DscItem/>
            <QtyItem>1</QtyItem>
            <PrcItem>5000</PrcItem>
            <MontoItem>5000</MontoItem>
        </Detalle>
        <Detalle>
            <NroLinDet>3</NroLinDet>
            <CdgItem>
                <TpoCodigo>INT1</TpoCodigo>
                <VlrCodigo>1515</VlrCodigo>
            </CdgItem>
            <NmbItem>Caja de Diskettes 10 Unidades</NmbItem>
            <DscItem/>
            <QtyItem>5</QtyItem>
            <PrcItem>1000</PrcItem>
            <MontoItem>5000</MontoItem>
        </Detalle>
        <TED version="1.0">
            <DD>
                <RE>97975000-5</RE>
                <TD>33</TD>
                <F>60</F>
                <FE>2003-10-13</FE>
                <RR>77777777-7</RR>
                <RSR>EMPRESA  LTDA</RSR>
                <MNT>119000</MNT>
                <IT1>Parlantes Multimedia 180W.</IT1>
                <CAF version="1.0">
                    <DA>
                        <RE>97975000-5</RE>
                        <RS>RUT DE PRUEBA</RS>
                        <TD>33</TD>
                        <RNG>
                            <D>1</D>
                            <H>200</H>
                        </RNG>
                        <FA>2003-09-04</FA>
                        <RSAPK>
                            <M>0a4O6Kbx8Qj3K4iWSP4w7KneZYeJ+g/prihYtIEolKt3cykSxl1zO8vSXu397QhTmsX7SBEudTUx++2zDXBhZw==</M>
                            <E>Aw==</E>
                        </RSAPK>
                        <IDK>100</IDK>
                    </DA>
                    <FRMA algoritmo="SHA1withRSA">g1AQX0sy8NJugX52k2hTJEZAE9Cuul6pqYBdFxj1N17umW7zG/hAavCALKByHzdYAfZ3LhGTXCai5zNxOo4lDQ==</FRMA>
                </CAF>
                <TSTED>2003-10-13T09:33:20</TSTED>
            </DD>
            <FRMT algoritmo="SHA1withRSA">GbmDcS9e/jVC2LsLIe1iRV12Bf6lxsILtbQiCkh6mbjckFCJ7fj/kakFTS06Jo8i
S4HXvJj3oYZuey53Krniew==</FRMT>
        </TED>
        <TmstFirma>2003-10-13T09:33:20</TmstFirma>
    </Documento>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#F60T33">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>hlmQtu/AyjUjTDhM3852wvRCr8w=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>JG1Ig0pvSIH85kIKGRZUjkyX6CNaY08Y94j4UegTgDe8+wl61GzqjdR1rfOK9BGn93AMOo6aiAgolW0k/XklNVtM/ZzpNNS3d/fYVa1q509mAMSXbelxSM3bjoa7H6Wzd/mV1PpQ8zK5gw7mgMMP4IKxHyS92G81GEguSmzcQmA=</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>
tNEknkb1kHiD1OOAWlLKkcH/UP5UGa6V6MYso++JB+vYMg2OXFROAF7G8BNFFPQx
iuS/7y1azZljN2xq+bW3bAou1bW2ij7fxSXWTJYFZMAyndbLyGHM1e3nVmwpgEpx
BHhZzPvwLb55st1wceuKjs2Ontb13J33sUb7bbJMWh0=
</Modulus>
<Exponent>
AQAB
</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>MIIEgjCCA+ugAwIBAgIEAQAApzANBgkqhkiG9w0BAQUFADCBtTELMAkGA1UEBhMC
Q0wxHTAbBgNVBAgUFFJlZ2lvbiBNZXRyb3BvbGl0YW5hMREwDwYDVQQHFAhTYW50
aWFnbzEUMBIGA1UEChQLRS1DRVJUQ0hJTEUxIDAeBgNVBAsUF0F1dG9yaWRhZCBD
ZXJ0aWZpY2Fkb3JhMRcwFQYDVQQDFA5FLUNFUlRDSElMRSBDQTEjMCEGCSqGSIb3
DQEJARYUZW1haWxAZS1jZXJ0Y2hpbGUuY2wwHhcNMDMxMDAxMTg1ODE1WhcNMDQw
OTMwMDAwMDAwWjCBuDELMAkGA1UEBhMCQ0wxFjAUBgNVBAgUDU1ldHJvcG9saXRh
bmExETAPBgNVBAcUCFNhbnRpYWdvMScwJQYDVQQKFB5TZXJ2aWNpbyBkZSBJbXB1
ZXN0b3MgSW50ZXJub3MxDzANBg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oeyVWAnhmuMZfReWQi
MUEHGGIW</X509Certificate>
</X509Data>
</KeyInfo>
</Signature></DTE>
</SetDTE><Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#SetDoc">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>4OTWXyRl5fw3htjTyZXQtYEsC3E=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>sBnr8Yq14vVAcrN/pKLD/BrqUFczKMW3y1t3JOrdsxhhq6IxvS13SgyMXbIN/T9ciRaFgNabs3pi732XhcpeiSmD1ktzbRctEbSIszYkFJY49k0eB+TVzq3eVaQr4INrymfuOnWj78BZcwKuXvDy4iAcx6/TBbAAkPFwMP9ql2s=</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>
tNEknkb1kHiD1OOAWlLKkcH/UP5UGa6V6MYso++JB+vYMg2OXFROAF7G8BNFFPQx
iuS/7y1azZljN2xq+bW3bAou1bW2ij7fxSXWTJYFZMAyndbLyGHM1e3nVmwpgEpx
BHhZzPvwLb55st1wceuKjs2Ontb13J33sUb7bbJMWh0=
</Modulus>
<Exponent>
AQAB
</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>MIIEgjCCA+ugAwIBAgIEAQAApzANBgkqhkiG9w0BAQUFADCBtTELMAkGA1UEBhMC
Q0wxHTAbBgNVBAgUFFJlZ2lvbiBNZXRyb3BvbGl0YW5hMREwDwYDVQQHFAhTYW50
aWFnbzEUMBIGA1UEChQLRS1DRVJUQ0hJTEUxIDAeBgNVBAsUF0F1dG9yaWRhZCBD
ZXJ0aWZpY2Fkb3JhMRcwFQYDVQ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lmaWNhZG8gcGFyYSB1
c28gdHJpYnV0YXJpbywgcGFnb3MsIGNvbWVyY2lvIHkgb3Ryb3MwCwYDVR0PBAQD
AgTwMA0GCSqGSIb3DQEBBQUAA4GBABMfCyJF0mNXcov8iEWvjGFyyPTsXwvsYbbk
OJ41wjaGOFMCInb4WY0ngM8BsDV22bGMs8oLyX7rVy16bGA8Z7WDUtYhoOM7mqXw
/Hrpqjh3JgAf8zqdzBdH/q6mAbdvq/yb04JHKWPC7fMFuBoeyVWAnhmuMZfReWQi
MUEHGGIW</X509Certificate>
</X509Data>
</KeyInfo>
</Signature></EnvioDTE>

2 个答案:

答案 0 :(得分:0)

您的查询很棒,但您必须指定默认名称空间xmlns

试试这样:

WITH XMLNAMESPACES(DEFAULT 'http://www.sii.cl/SiiDte')
INSERT INTO [dbo].[Ejemplo2]
select  a.value(N'(./RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
    a.value(N'(./RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
    a.value(N'(./RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
    a.value(N'(./FchResol)[1]', N'nvarchar(12)') as [FchResol],
    a.value(N'(./NroResol)[1]', N'nvarchar(12)') as [NroResol]
from @messagebody.nodes('/EnvioDTE/SetDTE/Caratula') as r(a);

答案 1 :(得分:0)

这个作品。你添加&#34; *:&#34;对于节点。

DECLARE @messagebody XML

SELECT @messagebody = BulkColumn
FROM OPENROWSET(BULK 'C:\Ejemplo.xml', SINGLE_CLOB) AS X

PRINT CONVERT(VARCHAR(MAX),@messagebody)

INSERT INTO [dbo].[Ejemplo24]
select  a.value(N'(./*:RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
    a.value(N'(./*:RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
    a.value(N'(./*:RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
    a.value(N'(./*:FchResol)[1]', N'nvarchar(12)') as [FchResol],
    a.value(N'(./*:NroResol)[1]', N'nvarchar(12)') as [NroResol]
from @messagebody.nodes('/*:EnvioDTE/*:SetDTE/*:Caratula') as r(a);