从存储过程从oracle表生成xml文件

时间:2014-02-27 06:13:58

标签: sql xml oracle

我创建了一个存储过程,以按照requrie格式生成xml文件 我在数据库表中找到了特殊的char,所以它不能处理到xml。 如何管理生成XML文件的特殊字符,我想要表的所有数据(面临clob大小问题) 请在这里帮忙

<XML>
    <TABLENAME></TABLENAME>
    <RECORDS>
    <RECORD>
              <COLNAME>AAA</COLNAME>
              <COLNAME>AAA</COLNAME>
    <RECORD>
    <RECORD>
              <COLNAME>AAA</COLNAME>
              <COLNAME>AAA</COLNAME>
    <RECORD>
    <RECORD>
              <COLNAME>AAA</COLNAME>
              <COLNAME>AAA</COLNAME>
    <RECORD>
    <RECORD>
              <COLNAME>AAA</COLNAME>
              <COLNAME>AAA</COLNAME>
    <RECORD>
    <RECORD>
              <COLNAME>AAA</COLNAME>
              <COLNAME>AAA</COLNAME>
    <RECORD>
    </RECORDS>
    </XML> 


<TABLENAME>  = actual table name(case should be same as in database)
<COLNAME> = actual column names from table(case should be same as in database)
Keep other tags as it is. Repeat <RECORD> tag for each row retrieved.


create or replace 
procedure dump_pcd (
   V_TABLE_NAME IN varchar2,
    v_FLAG OUT NUMBER
)
AS

BEGIN
    DECLARE
    xt_data xmltype;
    v_ctx dbms_xmlgen.ctxHandle;
    rc_data sys_refcursor;
    v_file  UTL_FILE.file_type;

    BEGIN 
   -- v_file := UTL_FILE.fopen('MYXML', V_TABLE_NAME ||'.xml', 'A');       
     --  v_file := UTL_FILE.fopen('MYXML', V_TABLE_NAME || '.xml', 'A');


      v_file := UTL_FILE.fopen('MYXML', V_TABLE_NAME || '.xml', 'W');
       UTL_FILE.put_line(v_file, '<XML><'||V_TABLE_NAME||'></'||V_TABLE_NAME||'> <RECORDS>');


    OPEN rc_data FOR 
    'select LOG_ID, USER_ID,RUN_DATE,PROCESS,MPOID,MODE_,STATUS,
                  regexp_replace(unistr(NOTES), ''[[:punct:]]'','''') as NOTES,     MARKDELETED from '|| V_TABLE_NAME||'  ';

    v_ctx := dbms_xmlgen.newContext (rc_data);
    DBMS_XMLGEN.SETNULLHANDLING(v_ctx,null);

    DBMS_XMLGEN.setrowsettag(v_ctx, 'RECORDS'); 
    DBMS_XMLGEN.setrowtag(v_ctx, 'RECORD');

     xt_data := dbms_xmlgen.getXMLType (v_ctx);
     dbms_xmlgen.closeContext (v_ctx);

    dbms_xslprocessor.clob2file(xt_data.getclobval( ), 'MYXML', ''||V_TABLE_NAME||'.xml');
    v_FLAG := 1;


      EXCEPTION
      WHEN OTHERS THEN
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           dbms_xmlgen.closeContext (v_ctx);
            v_FLAG := 0;

      end ;

END dump_pcd;

0 个答案:

没有答案