将带有pl sql的xml文件解析为2个oracle header-detail表

时间:2015-08-11 11:16:13

标签: sql xml oracle parsing plsql

我正在尝试通过pl sql解析xml文件。有一个标题部分(GrpHdr)带有一个Id元素,它应该被插入一个特定的表中,它的细节(Ntfctn),其元素将插入另一个表中,外键是第一个表的Id。该文件如下:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<DMCTfh:DIASMCTFH xmlns:DMCTfh="urn:DMCTfh:xsd:$DIASMCTFH">
  <DMCTfh:DIASFileHdr>
    <DMCTfh:SndgInst>DIASGRA1</DMCTfh:SndgInst>
    <DMCTfh:RcvgInst>90874</DMCTfh:RcvgInst>
    <DMCTfh:FileRef>0252013352000001</DMCTfh:FileRef>
    <DMCTfh:SrvcID>DCT</DMCTfh:SrvcID>
    <DMCTfh:TstCode>T</DMCTfh:TstCode>
    <DMCTfh:FType>XCT</DMCTfh:FType>
    <DMCTfh:NumGrp>1</DMCTfh:NumGrp>
  </DMCTfh:DIASFileHdr>
  <DMCTfh:BkToCstmrDbtCdtNtfctn xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.03">
    <GrpHdr>
      <MsgId>024201335200000000000132</MsgId>
      <CreDtTm>2013-05-24T09:35:10</CreDtTm>
      <MsgRcpt>
        <Id>
          <OrgId>
            <Othr>
              <Id>90874</Id>
              <Issr>DIAS</Issr>
            </Othr>
          </OrgId>
        </Id>
      </MsgRcpt>
    </GrpHdr>
    <Ntfctn>
      <Id>024201335200000000000129</Id>
      <CreDtTm>2013-05-24T09:35:10</CreDtTm>
      <Acct>
        <Id>
          <IBAN>GR6302602320000590200493673</IBAN>
        </Id>
        <Svcr>
          <FinInstnId>
            <BICFI>ERBKGRAA</BICFI>
          </FinInstnId>
        </Svcr>
      </Acct>
      <TxsSummry>
        <TtlNtries>
          <NbOfNtries>1</NbOfNtries>
        </TtlNtries>
      </TxsSummry>
      <Ntry>
        <Amt Ccy="EUR">100.00</Amt>
        <CdtDbtInd>CRDT</CdtDbtInd>
        <Sts>BOOK</Sts>
        <ValDt>
          <Dt>2013-05-24</Dt>
        </ValDt>
        <BkTxCd>
          <Domn>
            <Cd>PMNT</Cd>
            <Fmly>
              <Cd>CNTR</Cd>
              <SubFmlyCd>CDPT</SubFmlyCd>
            </Fmly>
          </Domn>
        </BkTxCd>
        <NtryDtls>
          <Btch>
            <NbOfTxs>1</NbOfTxs>
            <TtlAmt Ccy="EUR">100.00</TtlAmt>
          </Btch>
          <TxDtls>
            <Refs>
              <InstrId>01913351000006</InstrId>
              <EndToEndId>NOTPROVIDED</EndToEndId>
              <TxId>CRED-874-0208-2</TxId>
            </Refs>
            <Amt Ccy="EUR">100.00</Amt>
            <CdtDbtInd>CRDT</CdtDbtInd>
            <RltdPties>
              <Dbtr>
                <Nm>Debtor-NAME-301-01-1</Nm>
              </Dbtr>
            </RltdPties>
            <RltdAgts>
              <DbtrAgt>
                <FinInstnId>
                  <BICFI>ETHNGRAA</BICFI>
                </FinInstnId>
              </DbtrAgt>
            </RltdAgts>
            <RmtInf>
              <Ustrd>00000042280000087428</Ustrd>
            </RmtInf>
            <RltdDts>
              <AccptncDtTm>2013-05-24T00:00:00</AccptncDtTm>
            </RltdDts>
          </TxDtls>
        </NtryDtls>
      </Ntry>
    </Ntfctn>
    <Ntfctn>
      <Id>024201335200000000000130</Id>
      <CreDtTm>2013-05-24T09:35:10</CreDtTm>
      <Acct>
        <Id>
          <IBAN>GR7301723510005351020742556</IBAN>
        </Id>
        <Svcr>
          <FinInstnId>
            <BICFI>PIRBGRAA</BICFI>
          </FinInstnId>
        </Svcr>
      </Acct>
      <TxsSummry>
        <TtlNtries>
          <NbOfNtries>1</NbOfNtries>
        </TtlNtries>
      </TxsSummry>
      <Ntry>
        <Amt Ccy="EUR">100.00</Amt>
        <CdtDbtInd>CRDT</CdtDbtInd>
        <Sts>BOOK</Sts>
        <ValDt>
          <Dt>2013-05-24</Dt>
        </ValDt>
        <BkTxCd>
          <Domn>
            <Cd>PMNT</Cd>
            <Fmly>
              <Cd>RCDT</Cd>
              <SubFmlyCd>ESCT</SubFmlyCd>
            </Fmly>
          </Domn>
        </BkTxCd>
        <NtryDtls>
          <Btch>
            <NbOfTxs>1</NbOfTxs>
            <TtlAmt Ccy="EUR">100.00</TtlAmt>
          </Btch>
          <TxDtls>
            <Refs>
              <InstrId>01913351000003</InstrId>
              <EndToEndId>NOTPROVIDED</EndToEndId>
              <TxId>DD087401305240001</TxId>
            </Refs>
            <Amt Ccy="EUR">100.00</Amt>
            <CdtDbtInd>CRDT</CdtDbtInd>
            <RltdPties>
              <Dbtr>
                <Nm>ΔΕΛΗΑΝΤΩΝΗ ΜΑΡΙΑ</Nm>
              </Dbtr>
            </RltdPties>
            <RltdAgts>
              <DbtrAgt>
                <FinInstnId>
                  <BICFI>GPSBGRAA</BICFI>
                </FinInstnId>
              </DbtrAgt>
            </RltdAgts>
            <RmtInf>
              <Ustrd>00000047780000087424</Ustrd>
            </RmtInf>
            <RltdDts>
              <AccptncDtTm>2013-05-24T00:00:00</AccptncDtTm>
            </RltdDts>
          </TxDtls>
        </NtryDtls>
      </Ntry>
    </Ntfctn>
    <Ntfctn>
      <Id>024201335200000000000131</Id>
      <CreDtTm>2013-05-24T09:35:10</CreDtTm>
      <Acct>
        <Id>
          <IBAN>GR9801100400000004050700154</IBAN>
        </Id>
        <Svcr>
          <FinInstnId>
            <BICFI>ETHNGRAA</BICFI>
          </FinInstnId>
        </Svcr>
      </Acct>
      <TxsSummry>
        <TtlNtries>
          <NbOfNtries>1</NbOfNtries>
        </TtlNtries>
      </TxsSummry>
      <Ntry>
        <Amt Ccy="EUR">600.00</Amt>
        <CdtDbtInd>CRDT</CdtDbtInd>
        <Sts>BOOK</Sts>
        <ValDt>
          <Dt>2013-05-24</Dt>
        </ValDt>
        <BkTxCd>
          <Domn>
            <Cd>PMNT</Cd>
            <Fmly>
              <Cd>CNTR</Cd>
              <SubFmlyCd>CDPT</SubFmlyCd>
            </Fmly>
          </Domn>
        </BkTxCd>
        <NtryDtls>
          <Btch>
            <NbOfTxs>2</NbOfTxs>
            <TtlAmt Ccy="EUR">600.00</TtlAmt>
          </Btch>
          <TxDtls>
            <Refs>
              <InstrId>01913351000001</InstrId>
              <EndToEndId>NOTPROVIDED</EndToEndId>
              <TxId>CRED-874-0208-1</TxId>
            </Refs>
            <Amt Ccy="EUR">100.00</Amt>
            <CdtDbtInd>CRDT</CdtDbtInd>
            <RltdPties>
              <Dbtr>
                <Nm>Debtor-NAME-301-01-1</Nm>
              </Dbtr>
            </RltdPties>
            <RltdAgts>
              <DbtrAgt>
                <FinInstnId>
                  <BICFI>CRBAGRAA</BICFI>
                </FinInstnId>
              </DbtrAgt>
            </RltdAgts>
            <RmtInf>
              <Ustrd>00000042270000087428</Ustrd>
            </RmtInf>
            <RltdDts>
              <AccptncDtTm>2013-05-24T00:00:00</AccptncDtTm>
            </RltdDts>
          </TxDtls>
          <TxDtls>
            <Refs>
              <InstrId>01913352000001</InstrId>
              <EndToEndId>3121990500874553</EndToEndId>
              <TxId>61229720000000291396</TxId>
            </Refs>
            <Amt Ccy="EUR">500.00</Amt>
            <CdtDbtInd>CRDT</CdtDbtInd>
            <RltdPties>
              <Dbtr>
                <Nm>ΠΑΠΑΙΟΡΔΑΝΟΥ ΠΑΥΛΟΣ</Nm>
              </Dbtr>
            </RltdPties>
            <RltdAgts>
              <DbtrAgt>
                <FinInstnId>
                  <BICFI>CITIGRAA</BICFI>
                </FinInstnId>
              </DbtrAgt>
            </RltdAgts>
            <RmtInf>
              <Ustrd>00000042270000087428</Ustrd>
            </RmtInf>
            <RltdDts>
              <AccptncDtTm>2013-05-22T00:00:00</AccptncDtTm>
            </RltdDts>
          </TxDtls>
        </NtryDtls>
      </Ntry>
    </Ntfctn>
  </DMCTfh:BkToCstmrDbtCdtNtfctn>
</DMCTfh:DIASMCTFH>

我是一个新手,这是一个精心制作的文件,所以我不知道该怎么做,即使我查看了一些与我自己类似的帖子。任何帮助将不胜感激。

到目前为止,我已经得到了这个:

DECLARE
  xmlClob CLOB;
  xmlFile BFILE;
  x XMLType;

  src_offset number := 1 ;
  dest_offset number := 1 ;
  lang_ctx number := DBMS_LOB.DEFAULT_LANG_CTX;
  warning integer;
BEGIN
  xmlFile := BFILENAME('Parse_XML', 'DCT9087413052401.XOO');
  DBMS_LOB.CREATETEMPORARY(xmlClob, true);
  DBMS_LOB.FILEOPEN(xmlFile, DBMS_LOB.FILE_READONLY);
  DBMS_LOB.LOADCLOBFROMFILE(xmlClob, xmlFile, DBMS_LOB.LOBMAXSIZE, src_offset,
    dest_offset, DBMS_LOB.DEFAULT_CSID, lang_ctx, warning);
  x := XMLType.createXML(xmlClob);
  DBMS_LOB.FILECLOSEALL();
  DBMS_LOB.FREETEMPORARY(xmlClob);

  FOR r IN (
   SELECT ExtractValue(Value(p),'/Ntfctn/Id/text()') as Ident
          ,ExtractValue(Value(p),'/Ntfctn/CreDtTm/text()') as Creation_Dt
          ,ExtractValue(Value(p),'/Ntfctn/Acct/Id/IBAN/text()') as IBAN
          ,ExtractValue(Value(p),'/Ntfctn/Acct/Svcr/FinInstnId/BICFI/text()') as BIC
          ,ExtractValue(Value(p),'/Ntfctn/TxsSummry/TtlNtries/NbOfNtries/text()') as Entries_No
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/Amt/text()') as Entry_Amnt
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/CdtDbtInd/text()') as Entry_Ind
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/Sts/text()') as Entry_Status
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/Sts/ValDt/Dt/text()') as Val_Dt
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/BkTxCd/Domn/Cd/text()') as Cd
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/BkTxCd/Domn/Fmly/Cd/text()') as Fmly_Cd
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/BkTxCd/Domn/Fmly/SubFmlyCd/text()') as Subfmly_Cd
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/Btch/NbOfTxs/text()') as Trx_Cnt
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/Btch/TtlAmt/text()') as Ttl_Amnt
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/Refs/InstrId/text()') as Instr_Id
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/Refs/EndToEndId/text()') as End_Id
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/Refs/TxId/text()') as Trx_Id
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/Amt/text()') as Dtls_Amnt
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/CdtDbtInd/text()') as Dtls_Ind
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/RltdPties/Dbtr/Nm/text()') as Dbtor_Name
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/RltdAgts/DbtrAgt/FinInstnId/BICFI/text()') as Inst_BIC
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/RmtInf/Ustrd/text()') as Ustrd
          ,ExtractValue(Value(p),'/Ntfctn/Ntry/NtryDtls/TxDtls/RltdDts/AccptncDtTm/text()') as Accept_Dt
    FROM   TABLE(XMLSequence(Extract(x,'/Ntfctn'))) p
   WHERE ExtractValue(Value(p),'/Ntfctn/text()') = 'CUZK'
   ) LOOP
          INSERT INTO DIAS_INCOMING_DT (FK_FILE_ID, ID_ORDER, DIAS_IDENTITY, BANK_IDENTITY, ORDER_IDENTITY, AMOUNT, CURRENCY, DEBITOR_NAME, BIC_DB_BANK,
                                   BIC_IDENTITY, REMITTANCE_INFO, BANK_ACCEPT_DATE, MESSAGE_IDENTITY, MESSAGE_DATE, TRX_DATE, COMMENTS, ACCOUNT_NUMBER,
                                     MOVEMENT_ID, PRS_STATUS, PRS_TMSTAMP, PRS_ERROR, FK_FILE_ID)
                            VALUES (1, trim(r.End_Id), trim(r.Ident), trim(r.IBAN), trim(r.Ustrd), to_number(r.Entry_Amnt), 'EUR', trim(r.Dbtor_Name), '1',
                                      '1', '1', to_date(trim(r.Accept_Dt), 'YYYYMMDDHH24MISS'), trim(r.Trx_Id), to_date(trim(r.Val_Dt), 'YYYYMMDDHH24MISS'),
                                      to_date(trim(r.Val_Dt), 'YYYYMMDDHH24MISS'), '1', 1, 1, 1, to_date(trim(r.Creation_Dt), 'YYYYMMDDHH24MISS'), 'no_error', '1');
                      commit;
   END LOOP;
 END;

1 个答案:

答案 0 :(得分:0)

您可以使用XMLTABLE函数从XML中提取行。作为测试,我创建了一个名为“text”的表,其中一列名为“xmldata”。

然后,您可以像这样查询XML文档中的数据:

select header.id, data.*
from test, xmltable(-- GrpHdr as table
        xmlnamespaces(default 'urn:iso:std:iso:20022:tech:xsd:camt.054.001.03', 'urn:DMCTfh:xsd:$DIASMCTFH' as "DMCTfh" ),
        '/DMCTfh:DIASMCTFH/DMCTfh:BkToCstmrDbtCdtNtfctn/GrpHdr' 
        passing test.xmldata
        columns id varchar2(50) path 'MsgRcpt/Id/OrgId/Othr/Id'
    ) as header, 
    xmltable(-- Ntfctn as table
        xmlnamespaces(default 'urn:iso:std:iso:20022:tech:xsd:camt.054.001.03', 'urn:DMCTfh:xsd:$DIASMCTFH' as "DMCTfh" ),
        '/DMCTfh:DIASMCTFH/DMCTfh:BkToCstmrDbtCdtNtfctn/Ntfctn' 
        passing test.xmldata
        columns iban varchar2(50) path 'Acct/Id/IBAN',
            dt varchar2(50) path 'Ntry/ValDt/Dt' -- and so on
    ) as data;