Extracting Table Data Using SELECT Statement into an XML File

create or replace procedure
getXML(newContext_qry varchar2,
       rowSettag varchar2,
       rowTag varchar2,filename varchar2)
 is
-- Input query string

-- Input rowsetTag , the root tag
-- Input row level tag
-- Input file name
-- Note make sure that before running this procedure,
-- it is required that UTL_FILE_DIR must
-- be set in init.ora file In this code
-- it has been set to d:\oracle9i. 

begin
declare
 qryCtx dbms_xmlgen.ctxHandle ;
 result clob;
 lob_length     integer;
 read_amount    integer;
 read_offset    integer;

 buffer         varchar2(100);
 loc            varchar2(100) := 'usr_dir';
 f_hand         utl_file.file_type;

Begin
        -- Setting up offset and no. of chars to be read in
        -- in one go from clob datatype.
        read_offset := 1;

        read_amount := 75;

        dbms_output.put_line('opening');
        --Opening file
        f_hand := Utl_File.Fopen(location =>'d:\oracle9i',
                                 filename =>filename,
                                 open_mode =>'w',
                                 max_linesize => 32767);
        dbms_output.put_line('file open');

        -- Creating new context
        qryCtx := dbms_xmlgen.newContext(newContext_qry);

        -- Defining Rowsettag
        DBMS_XMLGEN.setRowSetTag(qryCtx,rowSettag);

        -- Defining Rowtag
        DBMS_XMLGEN.setRowTag(qryCtx,rowTag);

        -- Generating XML and storing in an clob datatype
        result :=DBMS_XMLGEN.getXML(qryCtx);

        dbms_output.put_line('xml generated');

        -- Getting the length of the data stored in Clob
        lob_length := dbms_lob.getlength(result);

        -- Reading data from clob variable and writng into file.
        while (lob_length > 0) loop
        dbms_lob.read(result,read_amount,read_offset,buffer);

        dbms_output.put_line('writing in file');
        utl_file.put(f_hand,buffer);
        dbms_output.put_line('written');
        read_offset := read_offset+read_amount;
        lob_length := lob_length-read_amount;
        if lob_length < read_amount then
           read_amount := lob_length;
        end if;
        end loop;
        utl_file.fclose(f_hand);

        EXCEPTION WHEN UTL_FILE.INVALID_PATH THEN
        RAISE_APPLICATION_ERROR(-20100,'Invalid Path');

        WHEN UTL_FILE.INVALID_MODE THEN
        RAISE_APPLICATION_ERROR(-20101,'Invalid Mode');

        WHEN UTL_FILE.INVALID_OPERATION then
        RAISE_APPLICATION_ERROR(-20102,'Invalid Operation');

        WHEN UTL_FILE.INVALID_FILEHANDLE then
        RAISE_APPLICATION_ERROR(-20103,'Invalid Filehandle');

        WHEN UTL_FILE.WRITE_ERROR then
        RAISE_APPLICATION_ERROR(-20104,'Write Error');

        WHEN UTL_FILE.READ_ERROR then
        RAISE_APPLICATION_ERROR(-20105,'Read Error');

        WHEN UTL_FILE.INTERNAL_ERROR then
        RAISE_APPLICATION_ERROR(-20106,'Internal Error');

        WHEN OTHERS THEN
        UTL_FILE.FCLOSE(f_hand);

end;

end;
/

Author Profile

sysdba ;

Other posts by sysdba

Author's web site



Are you satisfied with this blog?
Why not subscribe our RSS Feed? you will always get the latest post.


3 Comments

  1. exchangecards

    It would be more interesting if the author left the Yahoo Messenger We could discuss this theme more deeply

    1
  2. Download Movies

    Hi
    awesome post – i’m creating video about it and i will post it to youtube !
    if you wana to help or just need a link send me email !

    2
  3. South-Dakota-Insurance-559

    hm. hope to see same more info. Can we speake about it?

    3

Leave A Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



sponsored link