SET SERVEROUTPUT ON
DECLARE
  v_file  Utl_File.File_Type;
  v_xml   CLOB;
  v_more  BOOLEAN := TRUE;
BEGIN
  -- Create XML document from query.
  v_xml := DBMS_XMLQuery.GetXML('SELECT table_name, tablespace_name FROM user_tables WHERE rownum < 6');
  
  -- 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;
/
