Skip to Main Content
  • Questions
  • Generating XML files from clob field

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Raphael.

Asked: November 03, 2017 - 6:13 pm UTC

Last updated: May 17, 2019 - 9:14 am UTC

Version: 11g

Viewed 10K+ times! This question is

You Asked

Dear

I'd like to know if it's possible to generate several XML files from a clob filed, for instance:
A) The clob field is in a XML format. I've just need to run a loop to export every single row in a new XML file.
B) These files need to be exported for a Windows network folder, such as C:/Test, in my local machine and not in the server where the Oracle is installed.


Regards,
Raphael

and Connor said...

Here is an example using select-into to process 1 file. You can easily amend this to being a for-loop.

SQL> drop table t purge;

Table dropped.

SQL> create table t ( x clob );

Table created.

SQL>
SQL> declare
  2    c clob;
  3  begin
  4    insert into t values ( empty_clob())
  5    returning x into c;
  6
  7    c := q'{<?xml version='1.0'  encoding='UTF8' ?>}<RESULTS>}';
  8    loop
  9      c := c || '<ROW><COLUMN NAME="EMPNO"><![CDATA[7369]]></COLUMN><COLUMN NAME="ENAME"><![CDATA[SMITH]]></COLUMN><COLUMN NAME="JOB"><![CDATA[CLERK]]></COLUMN><COLUMN NAME="MGR"><![CDATA[7902]]></COLUMN></ROW>';
 10      exit when length(c) > 90000;
 11    end loop;
 12    c := c || '</RESULTS>';
 13
 14    update t set x = c;
 15    commit;
 16  end;
 17  /

PL/SQL procedure successfully completed.

SQL> select length(x), x from t;

 LENGTH(X) X
---------- --------------------------------------------------------------------------------
     90190 <?xml version='1.0'  encoding='UTF8' ?>}<RESULTS><ROW><COLUMN NAME="EMPNO"><![CD

1 row selected.

SQL>
SQL> DECLARE
  2    Lv_cursor PLS_INTEGER;
  3    Lv_file UTL_FILE.file_type;
  4    P_SubstrVal VARCHAR2(4000) ;
  5    lv_file_name VARCHAR2 (100):='testing.csv';
  6    P_Directory VARCHAR2(101) :='TEMP';
  7    crlf VARCHAR2(2) := chr(10)||chr(13);
  8    l_columnValClob CLOB;
  9    P_offset NUMBER :=1;
 10    P_amount NUMBER := 3000;
 11    l_clobLen NUMBER :=0;
 12  BEGIN
 13    Lv_cursor := DBMS_SQL.open_cursor;
 14    SELECT x
 15    INTO l_columnValClob
 16    FROM t;
 17
 18    l_clobLen := DBMS_LOB.GETLENGTH(l_columnValClob);
 19    Lv_file := utl_file.fopen(P_Directory,lv_file_name,'wb');
 20    WHILE P_offset < l_clobLen
 21    LOOP
 22      P_SubstrVal := dbms_lob.substr(l_columnValClob,P_amount,P_offset);
 23      utl_file.put_raw(Lv_file,utl_raw.cast_to_raw(P_SubstrVal));
 24      P_offset:=P_offset+P_amount;
 25    END LOOP;
 26    UTL_FILE.fclose(Lv_file);
 27  END ;
 28  /

PL/SQL procedure successfully completed.

SQL>
SQL>


Now that will write the files to a folder that the *database* can access. Options to get it your local machine could be a shared folder that both database and clients can access.

If you need it exclusively to a local drive, you'd need to either

- write something yourself (eg Java)
- use SQL Developer, which has a "export as separate files" option

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

worked like a charm

panch, May 14, 2019 - 7:38 pm UTC

exactly what I was looking for, worked great.
Chris Saxon
May 17, 2019 - 9:14 am UTC

Great to hear.

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database