SET SERVEROUTPUT ON
DECLARE
  v_ctx   DBMS_XMLGen.ctxHandle;
  v_file  Utl_File.File_Type;
  v_xml   CLOB;
  v_more  BOOLEAN := TRUE;
BEGIN
  -- Create XML context.
  v_ctx := DBMS_XMLGen.newContext('SELECT table_name, tablespace_name FROM user_tables WHERE rownum < 6');
  
  -- Set parameters to alter default Rowset and Row tag names and default case.
  DBMS_XMLGen.setRowsetTag(v_ctx, 'USER_TABLES'); 
  DBMS_XMLGen.setRowTag(v_ctx, 'TABLE'); 
  --DBMS_XMLGen.setTagCase(v_ctx, DBMS_XMLGen.LOWER_CASE);
  
  -- Add an IE specfic XSL stylesheet reference so browser can transform the file.
  --DBMS_XMLGen.setStylesheetHeader(v_ctx, 'C:\Development\XML\IEStyle.xsl', 'text/xsl');

  -- Create the XML document.
  v_xml := DBMS_XMLGen.GetXML(v_ctx);
  DBMS_XMLGen.closeContext(v_ctx);
  
  -- Output XML document to file.
  v_file := Utl_File.FOpen('C:\Development\XML', 'test1.xml', 'w');
  WHILE v_more LOOP
    Utl_File.Put(v_file, Substr(v_xml, 1, 32767));
    IF Length(v_xml) > 32767 THEN
      v_xml :=  Substr(v_xml, 32768);
    ELSE
      v_more := FALSE;
    END IF;
  END LOOP;
  Utl_File.FClose(v_file);

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(Substr(SQLERRM,1,255));
    Utl_File.FClose(v_file);
END;
/
 

 
