无法从CLOB列写入正确的XML数据

时间:2015-06-29 15:08:58

标签: oracle plsql procedure

/ *我编写了以下过程,它将CLOB列数据写入XML格式的物理位置。该文件已成功写入,但被截断或缺少正确的XML格式。* /

CREATE OR REPLACE PROCEDURE p_generate_xml
IS

  c_amount         BINARY_INTEGER := 32767;
  l_buffer         VARCHAR2(32767);
  l_chr10          PLS_INTEGER;
  l_clobLen        PLS_INTEGER;
  l_fHandler       UTL_FILE.FILE_TYPE;
  l_pos            PLS_INTEGER    := 1;
 l_clob           CLOB;
 l_message_num number;
 l_cnt number;
 l_err_msg varchar2(3000);
 v_sysdate date;

BEGIN

l_message_num := 1;

   for c2 in ( SELECT xml_clob , case_id FROM AUDIT_XML_CLOB where case_id = '2006S1000018') 
   loop

   select  count(1) into l_cnt from AUDIT_XML_CLOB where nvl(DBMS_LOB.GETLENGTH(xml_clob),0) > 0;

    if l_cnt > 0 then 

      l_pos := 1;

       SELECT xml_clob
        INTO  l_clob 
        FROM AUDIT_XML_CLOB where case_id = c2.case_id;

          l_fHandler := UTL_FILE.FOPEN('MY_DIR1', 'test.xml','W',c_amount);

l_clobLen  := DBMS_LOB.GETLENGTH(l_clob);                             


             WHILE l_pos < l_clobLen
              LOOP

                l_buffer := DBMS_LOB.SUBSTR(l_clob, c_amount, l_pos);    

                EXIT WHEN l_buffer IS NULL;

 UTL_FILE.PUT_line(l_fHandler, l_buffer,TRUE);
                l_pos := l_pos + LEAST(LENGTH(l_buffer)+1,c_amount);
                dbms_output.put_line('l_pos:'||l_pos);  

UTL_FILE.FFLUSH (l_fHandler); 

END LOOP;

  UTL_FILE.FCLOSE(l_fHandler);

l_message_num := l_message_num +1;

end if;
    end loop;
commit;
dbms_output.put_line('4');
EXCEPTION
WHEN OTHERS THEN
  IF UTL_FILE.IS_OPEN(l_fHandler) THEN
    UTL_FILE.FCLOSE(l_fHandler);
  END IF;
  --RAISE;
l_err_msg:= substr(sqlerrm,1,3000);

dbms_output.put_line(l_err_msg);

END;
/

1 个答案:

答案 0 :(得分:0)

1)将clob转储到新文件的快速方法是使用DBMS_XSLPROCESSOR.clob2file(...)
2)你的xml有什么问题?

begin 
   for c2 in ( SELECT xml_clob , case_id FROM AUDIT_XML_CLOB where case_id = '2006S1000018') 
   loop
      dbms_xslprocessor.clob2file(CL=> c2.xml_clob, FLOCATION=>'MY_DIR1', FNAME=>'test.xml', CSID=>0);
   end loop;
end;

MY_DIR1存在且您有权在此位置书写。