1

I need to generate a XML and write it to a file. I have used utl_file.put_line. It creates the file, but nothing is written into it. Also, it shows no errors. I've already checked if I have the permissions to write on the directory. Code:

SET serveroutput ON;
DECLARE
    ctx DBMS_XMLGEN.CTXHANDLE;
    resposta CLOB;
    xml_file utl_file.file_type;
BEGIN
    xml_file:=utl_file.fopen ('DATA_PUMP_DIR', 'xml.txt', 'W');
    ctx := dbms_xmlgen.newContext ('select * from tb_museu M where M.cnpj=111111 OR   M.cnpj=222222');
    dbms_xmlgen.setRowsetTag (ctx, 'TODOS_OS_MUSEUS');
    dbms_xmlgen.setRowTag (ctx, 'MUSEU');
    resposta :=dbms_xmlgen.getXML (ctx);
    utl_file.put_line (xml_file,'teste');
    --utl_file.put_line (xml_file,resposta);
   dbms_xmlgen.closeContext(ctx);
END;
/
rvcam
  • 157
  • 5
  • 15
  • Just found out that I could use dbms_xslprocessor.clob2file, which works, to send my xml right to my file. But I still want to know why does utl_file.put_line doesn't work. – rvcam Feb 17 '14 at 03:24
  • 1
    maybe it's because you never closed the file with utl_file.fclose - utl_file keeps the data in a buffer and periodically flushes it to disk, or if you fclose. – Jeffrey Kemp Feb 17 '14 at 04:37
  • @JeffreyKemp Yes, that solved it. Put it as answer so I can vote for you. – rvcam Feb 19 '14 at 04:07

1 Answers1

1

For performance reasons UTL_FILE uses a buffer which is periodically flushed to the actual file on disk. Generally you don't need to worry about flushing, except you do need to close the file at the end with UTL_FILE.fclose(xmlfile);.

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158