generate xml file from oracle table

混江龙づ霸主 提交于 2020-01-06 08:32:53

问题


when I ran below store procedure then it shows error. here some special char related issue ,so i cant generate xml file , here xml format same as generate XML from oracle tables

Connecting to the database DB_old.
ORA-29285: file write error
ORA-06512: at "SYS.UTL_FILE", line 148
ORA-06512: at "SYS.UTL_FILE", line 889
ORA-06512: at "DBO_17FEB.EXPOR1", line 60
ORA-06512: at line 6
Process exited.
Disconnecting from the database DB_old.



-------------------



create or replace 
PROCEDURE Expor1 
(
   V_TABLE_NAME IN varchar2
  )
AS
BEGIN
         ----- Export  table data
      DECLARE
        v_file  UTL_FILE.file_type;
        qryCtx DBMS_XMLGEN.ctxHandle;
        result CLOB;
        result1 CLOB;
        v_FILENAME varchar2(30);
      BEGIN

       IF UPPER(V_TABLE_NAME) = 'PROJECT' THEN
      qryCtx :=  dbms_xmlgen.newContext ('SELECT * from '||V_TABLE_NAME ||'' );

       ELSIF UPPER(V_TABLE_NAME) = 'LOGFILE' THEN

      qryCtx :=  dbms_xmlgen.newContext ('select LOG_ID, USER_ID,RUN_DATE,PROCESS,MPOID,MODE_,trim(STATUS) as STATUS,
                                                 trim(regexp_replace(unistr(NOTES), ''[[:punct:]] '','''')) as NOTES, 
                                                 MARKDELETED from logfile where rownum<100 ' );

       ELSE
       qryCtx :=  dbms_xmlgen.newContext ('SELECT * from '||V_TABLE_NAME ||'' );
      END IF;


      v_FILENAME :=V_TABLE_NAME;
      DBMS_XMLGEN.setMaxRows(qryCtx, 5);
         v_file := UTL_FILE.fopen('MYXML', v_FILENAME || '.xml', 'W');
       UTL_FILE.put_line(v_file, '<XML><'||v_FILENAME||'></'||v_FILENAME||'> <RECORDS>');
       -- v_file := UTL_FILE.FOPEN('MYXML', v_FILENAME|| '.xml', 'R');

       LOOP
         DBMS_XMLGEN.SETNULLHANDLING(qryCtx ,null);
         DBMS_XMLGEN.setRowSetTag(qryCtx, 0);
         DBMS_XMLGEN.setRowTag(qryCtx, 'RECORD');

      -- save the XML into the CLOB field
       result :=  DBMS_XMLGEN.getXML(qryCtx);
       --result := REPLACE( result, '<?xml version="1.0"?>','<XML><'||v_FILENAME||'>'||result1 ||'</'||v_FILENAME||'>' );
         result := REPLACE( result, '<?xml version="1.0"?>',' ');
         result := REPLACE( result, '<_x0030_>',' ');
         result := REPLACE( result, '</_x0030_>',' '); 
         --result :=trim(result);
      -- UTL_FILE.put_line(v_file, '');
       EXIT WHEN DBMS_XMLGEN.getNumRowsProcessed(qryCtx) = 0; 
          -- store the XML to a XML files
       UTL_FILE.put_line(v_file, result);
      --UTL_FILE.put_line(v_file, '</XML>');
       END LOOP; 
      UTL_FILE.put_line(v_file, '</RECORDS></XML>');
       UTL_FILE.FCLOSE(v_file);

       END; 


END Expor1;

i am not able to handle some special char like : & / ; :/ . etc please help

回答1:


The ORA-29285: file write error isn't the same character-conversion error you referred to before, which was ORA-31061: XDB error: special char to escaped char conversion failed. It really isn't clear which error you're getting; if it's the file one then the character conversion isn't relevant.

When I run your version of my code against my table, it works OK for smaller tables, and gets ORA-06502: PL/SQL: numeric or value error if result is more than 32k characters; or ORA-29285 if there is more than 32k without a line break. You've lost the looping over the clob to write it to the file in chunks. To output larger values, as I did before:

position pls_integer := 1;
chars pls_integer := 32767;
...
while position < dbms_lob.getlength(result) loop
  utl_file.put(v_file, dbms_lob.substr(result, chars, position));
  utl_file.fflush(v_file);
  position := position + chars;
end loop;

Not sure why you have an inner block (declare/begin/`end') within the procedure.

If you are getting the ORA-31061 then I'm still unclear which data is causing you a problem, but assuming that the NOTES transformation you're doing solves that and you're still seeing ORA-31061 when you call this for your LOGFILE table, then that's probably because you reset qryCtx.

You're creating that at line 23 in the code you provided as:

      qryCtx :=  dbms_xmlgen.newContext ('select LOG_ID, 
        USER_ID,RUN_DATE,PROCESS,MPOID,MODE_,STATUS,
        regexp_replace(unistr(NOTES), ''[[:punct:]]'','''') as NOTES,
        MARKDELETED from logfile' );

... but then after the if/elsif/else block where you do that, you then overwrite it at line 39 with:

      qryCtx :=  dbms_xmlgen.newContext ('SELECT * from '||V_TABLE_NAME ||'' );

So when you then call getXML(qryCtx) you aren't getting the modified values for NOTES.



来源:https://stackoverflow.com/questions/22041833/generate-xml-file-from-oracle-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!