|
|
|
|
Re: How to Generate Xml file from Oracle [message #112666 is a reply to message #92533] |
Mon, 28 March 2005 11:35 |
bsc7080oqc
Messages: 2 Registered: March 2005 Location: Shelbyville, KY
|
Junior Member |
|
|
I created the following to generate an XML file from a provided query which then writes the file out. It can generate small or large all depending on your needs.
Also you may want to change the header components to suit your needs :
<TRANSACTION_SET><TRANSACTIONS><TRANSACTION>
Each row is encapsulated by <TROW>.
FUNCTION dump_xml (
p_query IN VARCHAR2
, p_dir IN VARCHAR2
, p_filename IN VARCHAR2
, p_max_linesize IN NUMBER DEFAULT 32000
, p_mode IN VARCHAR2 DEFAULT 'w'
)
RETURN NUMBER
IS
l_output UTL_FILE.file_type;
l_thecursor INTEGER DEFAULT DBMS_SQL.open_cursor;
l_columnvalue VARCHAR2 (4000);
l_status INTEGER;
l_colcnt NUMBER DEFAULT 0;
l_colcnt_array humcust.hum_toolbox.vc80_table;
l_colcnt_empty_array humcust.hum_toolbox.vc80_table;
l_cnt NUMBER DEFAULT 0;
l_separator VARCHAR2 (10) DEFAULT '';
l_line LONG;
l_desctbl DBMS_SQL.desc_tab;
v_sqlerrm VARCHAR2 (32000);
l_mode CHAR (1) := 'w';
BEGIN
l_colcnt_array := l_colcnt_empty_array;
IF p_mode NOT IN ('w', 'a')
THEN
l_mode := 'w';
ELSE
l_mode := p_mode;
END IF;
l_output :=
UTL_FILE.fopen (p_dir
, p_filename
, l_mode
, p_max_linesize
);
DBMS_SQL.parse (l_thecursor
, p_query
, DBMS_SQL.native
);
DBMS_SQL.describe_columns (l_thecursor
, l_colcnt
, l_desctbl
);
FOR i IN 1 .. l_colcnt
LOOP
DBMS_SQL.define_column (l_thecursor
, i
, l_columnvalue
, 4000
);
IF (l_desctbl (i).col_type = 2) /* number type */
THEN
l_desctbl (i).col_max_len := l_desctbl (i).col_precision
+ 2;
ELSIF (l_desctbl (i).col_type = 12) /* date type */
THEN
/* length of my date format */
l_desctbl (i).col_max_len := 20;
ELSIF (l_desctbl (i).col_type = /* LONG type */
THEN
l_desctbl (i).col_max_len := 2000;
END IF;
l_colcnt_array (i) := l_desctbl (i).col_name;
END LOOP;
l_status := DBMS_SQL.EXECUTE (l_thecursor);
UTL_FILE.put (l_output, '<TRANSACTION_SET><TRANSACTIONS><TRANSACTION>');
LOOP
EXIT WHEN (DBMS_SQL.fetch_rows (l_thecursor) <= 0);
l_line := NULL;
UTL_FILE.put (l_output, '<TROW>');
FOR i IN 1 .. l_colcnt
LOOP
DBMS_SQL.column_value (l_thecursor
, i
, l_columnvalue
);
UTL_FILE.put (l_output
, '<'
|| l_colcnt_array (i)
|| '>'
|| l_columnvalue
|| '</'
|| l_colcnt_array (i)
|| '>');
END LOOP;
UTL_FILE.put (l_output, '</TROW>');
UTL_FILE.new_line (l_output);
l_cnt := l_cnt
+ 1;
END LOOP;
UTL_FILE.put (l_output
, '</TRANSACTION></TRANSACTIONS></TRANSACTION_SET>');
DBMS_SQL.close_cursor (l_thecursor);
UTL_FILE.fclose (l_output);
RETURN l_cnt;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line ('NO_DATA_FOUND');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.invalid_path
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.INVALID_PATH');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.read_error
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.READ_ERROR');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.write_error
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.WRITE_ERROR');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.invalid_mode
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.INVALID_MODE');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.invalid_filehandle
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.INVALID_FILEHANDLE');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.invalid_operation
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.INVALID_OPERATION');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.internal_error
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.INTERNAL_ERROR');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN UTL_FILE.invalid_maxlinesize
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.INVALID_MAXLINESIZE');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('UTL_FILE.VALUE_ERROR');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
WHEN OTHERS
THEN
hum_do.default_exception ('ERROR in dump_csv : ');
UTL_FILE.fclose (l_output);
RETURN l_cnt;
END dump_xml;
|
|
|
Re: How to Generate Xml file from Oracle [message #116190 is a reply to message #92538] |
Mon, 18 April 2005 09:15 |
mmanza
Messages: 1 Registered: April 2005
|
Junior Member |
|
|
When I use the first option in SQL, I've not problem:
SELECT dbms_xmlquery.getXML('SELECT * FROM emp') FROM dual;
But if I try with:
x_xml := dbms_xmlquery.getxml('SELECT * FROM emp');
in Forms, my session is closed with:
PDE-PER001 Internal error (de pxc 3)
Could you help me?
|
|
|