如何将XML文件加载到Oracle表中?

时间:2018-07-10 03:53:45

标签: sql xml oracle plsql

我对此很陌生,所以请多多包涵。谢谢。

我的C驱动器中有一个目录。这样,C:/ Temp。它包含我要加载到名为xml_tab的Oracle表中的XML文件。

  CREATE TABLE "OMBIL"."XML_TAB" 
  ("ID" NUMBER(10,0), 
   "FILENAME" VARCHAR2(100 BYTE), 
   "XML" "XMLTYPE", 
    CONSTRAINT "XML_TAB_PK" PRIMARY KEY ("ID")
  )

我的XML文件具有相同的结构,如下所示

<?xml version="1.0" encoding="us-ascii"?>
<Plant name="Red Sea" schemaVersion="1.2">
   <Unit name="G #01">
     <BOps event_type="U1" cause_code="7110" amp_code="C0" begin_time="2018-06-01T07:08:00">XXXXXXXX</BOps>
    <BOps event_type="GE" cause_code="0001" begin_time="2018-06-01T11:20:33" />
    <BOps event_type="PO" cause_code="7110" amp_code="C0" begin_time="2018-06-20T10:01:00">YTYYYYYYYY</BOps>
    <BOps event_type="GE" cause_code="0001" begin_time="2018-06-20T14:48:46" />
    <Generation begin_time="2018-06-01T00:00:00" end_time="2018-06-30T23:59:59" xxx_produced="50592.90" yyy_consumed="0.00" zzzz_delivered="0.00" />
</Unit>
<Unit name="G #02">
    <Generation begin_time="2018-06-01T00:00:00" end_time="2018-06-30T23:59:59" xxx_produced="0.00" yyy_consumed="0.00" zzzz_delivered="0.00" />
</Unit>
<Unit name="G #03">
    <BOps event_type="GE" cause_code="0001" begin_time="2018-06-27T12:20:00">Something off grid</BOps>
    <BOps event_type="RS" cause_code="0000" begin_time="2018-06-27T12:21:00">open grid</BOps>
    <BOps event_type="GE" cause_code="0001" begin_time="2018-06-27T12:32:00">closed grid</BOps>
    <BOps event_type="RS" cause_code="0000" begin_time="2018-06-27T12:53:58" />
    <Generation begin_time="2018-06-01T00:00:00" end_time="2018-06-30T23:59:59" xxx_produced="25.34" yyy_consumed="0.00" zzzz_delivered="0.00" />
</Unit>
<StationServiceUsage begin_time="2018-06-01T00:00:00" end_time="2018-06-30T23:59:59" xxx="1944.30" />
</Plant>

我想编写一个PL / SQL过程以查看我的C:\ Temp目录,并将xml文件加载到xml_tab oracle表中。你们有没有可以效仿的例子?

我确实有以下代码,但是不能正常工作,我也希望代码循环遍历C:\ Temp目录中的所有xml文件,我该怎么做??

 create or replace PROCEDURE load_xml (p_dir       IN  VARCHAR2,
                                  p_filename  IN  VARCHAR2) AS
 l_bfile  BFILE := BFILENAME(p_dir, p_filename);
 l_clob   CLOB;

 l_dest_offset   INTEGER := 1;
 l_src_offset    INTEGER := 1;
 l_bfile_csid    NUMBER  := 0;
 l_lang_context  INTEGER := 0;
 l_warning       INTEGER := 0;
 BEGIN
  DBMS_LOB.createtemporary (l_clob, TRUE);

 DBMS_LOB.fileopen(l_bfile, DBMS_LOB.file_readonly);
 -- loadfromfile deprecated.
 -- DBMS_LOB.loadfromfile(l_clob, l_bfile, DBMS_LOB.getlength(l_bfile));
 DBMS_LOB.loadclobfromfile (
   dest_lob      => l_clob,
   src_bfile     => l_bfile,
   amount        => DBMS_LOB.lobmaxsize,
   dest_offset   => l_dest_offset,
   src_offset    => l_src_offset,
   bfile_csid    => l_bfile_csid ,
   lang_context  => l_lang_context,
   warning       => l_warning);
   DBMS_LOB.fileclose(l_bfile);

  INSERT INTO xml_tab (
   id,
   filename,
    xml
  )
 VALUES (
    xml_tab_seq.NEXTVAL,
    p_filename,
    XMLTYPE.createXML(l_clob)
 );
 COMMIT;

 DBMS_LOB.freetemporary (l_clob);
 END load_xml;

出现以下错误:

 BEGIN load_xml(p_dir => 'XML_DIR', p_filename => 'test.xml'); END;
Error report -
ORA-22288: file or LOB operation FILEOPEN failed
No such file or directory
ORA-06512: at "SYS.DBMS_LOB", line 805
ORA-06512: at "OMBIL.LOAD_XML", line 14
ORA-06512: at line 1
22288. 00000 -  "file or LOB operation %s failed\n%s"
*Cause:    The operation attempted on the file or LOB failed.
 *Action:   See the next error message in the error stack for more detailed
       information.  Also, verify that the file or LOB exists and that
       the necessary privileges are set for the specified operation. If
       the error still persists, report the error to the DBA.

我确实将XML_DIR上的所有内容都授予了公众。

谢谢。

1 个答案:

答案 0 :(得分:0)

似乎您的文件不在提供的目录中:

declare
   l_bfile  BFILE := BFILENAME('ORACLE_UTL_DIR', 'test.lst');
begin
   DBMS_LOB.fileopen(l_bfile, DBMS_LOB.file_readonly);
end; 

PL/SQL procedure successfully completed.

但文件不存在:

declare
   l_bfile  BFILE := BFILENAME('ORACLE_UTL_DIR', 'test.lstxxx');
begin
   DBMS_LOB.fileopen(l_bfile, DBMS_LOB.file_readonly);
end; 

Error report -
ORA-22288: file or LOB operation FILEOPEN failed
No such file or directory
ORA-06512: at "SYS.DBMS_LOB", line 805
ORA-06512: at line 4
22288. 00000 -  "file or LOB operation %s failed\n%s"
*Cause:     The operation attempted on the file or LOB failed.
*Action:    See the next error message in the error stack for more detailed
              information.  Also, verify that the file or LOB exists and that
              the necessary privileges are set for the specified operation. If
              the error still persists, report the error to the DBA.

要调试,可以使用:dbms_lob.fileexists(l_bfile)在打开文件之前。