DBMS_XMLDOM.WRITETOFILE overloading
Hello all. Quick question. I'm running into memory issues when creating my xml file using the DBMS_XMLDOM oracle 9i package. I'd like to write out the file initially and then append to it using DBMS_XMLDOM.WRITETOFILE throughout my program. Similar to "open_mode" using utl_file.fopen.
Is this possible?
In your example, how is the marco.xml file not getting written over each time with the next iteration of rc? Once you exit the loop, is that when the doc element is freed. Prior to that does the writetofile simply keep appending?
Here is a sample piece of my code
declare
doc dbms_xmldom.DOMDocument;
main_node dbms_xmldom.DOMNode;
root_node dbms_xmldom.DOMNode;
incident_node dbms_xmldom.DOMNode;
item_node dbms_xmldom.DOMNode;
root_elmt dbms_xmldom.DOMElement;
item_elmt dbms_xmldom.DOMElement;
item_text dbms_xmldom.DOMText;
v_clob CLOB := 'a';
v_xml VARCHAR2(32767);
output utl_file.file_type;
begin
-- get document
doc := dbms_xmldom.newDOMDocument;
-- create root element
main_node := dbms_xmldom.makeNode(doc);
root_elmt := dbms_xmldom.createElement(doc, 'Incidents');
dbms_xmldom.setAttribute(root_elmt, 'xmlns','http://www.claimsuite.com');
dbms_xmldom.setAttribute(root_elmt, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
dbms_xmldom.setAttribute(root_elmt, 'xsi:schemaLocation', 'http://www.claimsuite.com ClaimsuiteShellUS.xsd');
root_node := dbms_xmldom.appendChild(main_node, dbms_xmldom.makeNode(root_elmt));
for i in 1 .. 2 loop
item_elmt := dbms_xmldom.createElement(doc, 'Incident');
incident_node := dbms_xmldom.appendChild(root_node, dbms_xmldom.makeNode(item_elmt));
--CrawfordHandlingOfficeReference
item_elmt := dbms_xmldom.createElement(doc, 'HandlingOfficeReference');
item_node := dbms_xmldom.appendChild(incident_node, dbms_xmldom.makeNode(item_elmt));
item_text := dbms_xmldom.createTextNode(doc, 'HandlingOfficeRef');
item_node := dbms_xmldom.appendChild(item_node, dbms_xmldom.makeNode(item_text));
end loop;
-- instead of writing to file, it writes to clob
dbms_xmldom.writeToFile(doc, 'IO_DIR_SHL_RSG/test.xml','ISO-8859-1');
-- free resources
dbms_xmldom.freeDocument(doc);
end;Instead of having
dbms_xmldom.writeToFile(doc, 'IO_DIR_SHL_RSG/test.xml','ISO-8859-1');at the end, I'd like to continuously append to the test.xml file instead of storing the huge dbms_xmldom.DOMDocument in memory and then writing it out at the end.
Similar Messages
-
DBMS_XMLDOM.WRITETOFILE
Hi,
I am creating a PL/SQL that should read from an XML file in XML DB Repository, create an XML file out of it.
The lines I have in PL/SQL to create the XML file are:
v_domdoc DBMS_XMLDOM.DOMDOCUMENT;
v_domelement DBMS_XMLDOM.DOMELEMENT;
v_domnode DBMS_XMLDOM.DOMNODE;
v_domdoc := DBMS_XMLDOM.NEWDOMDOCUMENT();
l_string := '/PUBLIC/OUT/AC.XML';
DBMS_XMLDOM.WRITETOFILE(v_domnode, l_string);
When I run the PL/SQL, I get the following error at WRITETOFILE:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 33
ORA-06512: at "SYS.UTL_FILE", line 436
ORA-06512: at "XDB.DBMS_XSLPROCESSOR", line 217
ORA-29280: invalid directory path
ORA-29280: invalid directory path
What must I do in order to create the file successfully? By the way, '/PUBLIC/OUT' folder exists and AC.XML file does not exist.
Thanks in advance
BharathanSorry, I must have added these details in the first place.
Yes, I do have read/write access at OS (Windows Vista Home Premium). I am trying to create the file in XML DB Repository.
Following are the DB details:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
I am using SQL*Developer to develop the PL/SQL. Please bear with me if I am not providing you enough details. Appreciate your help coming my way.
Regards
Bharathan -
DBMS_XMLDOM.WRITETOFILE errors
C'mon then chaps (and chapesses)...
I've had DBMS_XMLDOM.WRITETOFILE working before but for some reason I just can't seem to get it to work again. (Using Oracle 10g R2)
What am I doing wrong. It's bound to be something right in front of me, but I just can't see it.
create or replace directory utl_data as 'C:\';
declare
xd DBMS_XMLDOM.DOMDocument;
xt XMLTYPE;
CURSOR cur_xml IS
select xmlelement("fred", 'fred') from dual;
begin
OPEN cur_xml;
FETCH cur_xml INTO xt;
CLOSE cur_xml;
xd := DBMS_XMLDOM.NewDOMDocument(xt);
DBMS_XMLDOM.WRITETOFILE(xd, 'C:\FRED.XML');
end;
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 33
ORA-06512: at "SYS.UTL_FILE", line 436
ORA-06512: at "XDB.DBMS_XSLPROCESSOR", line 217
ORA-29280: invalid directory path
ORA-29280: invalid directory path
ORA-06512: at "XDB.DBMS_XMLDOM", line 4416
ORA-06512: at line 12Well that's very nice of you to answer my thread after a year, but I already know about using directory objects now.
I'm not quite sure what you are trying to show with you examples, but good luck to you anyway. -
hi,
i have below block,
DECLARE
doc dbms_xmldom.DOMDocument;
node dbms_xmldom.DOMNode;
elem dbms_xmldom.DOMElement;
cur_node dbms_xmldom.DOMNode;
root_elem_data dbms_xmldom.DOMElement;
root_elem_tab dbms_xmldom.DOMElement;
root_node_data dbms_xmldom.DOMNode;
mode_elmn dbms_xmldom.DOMElement;
mode_node dbms_xmldom.DOMNode;
mode_text dbms_xmldom.DOMText;
doc1 DBMS_XMLDOM.DOMDOCUMENT;
root_node_data1 DBMS_XMLDOM.DOMNODE;
child_document DBMS_XMLDOM.DOMDOCUMENT;
child_rootnode DBMS_XMLDOM.DOMNODE;
V_CLOB CLOB;
v_doc CLOB;
v_EMP CLOB;
v_output_filename VARCHAR2(300) := 'SPOOL_DIR/'||'EMP_XML_FILE.xml';
l_xmltype XMLTYPE;
BEGIN
doc := dbms_xmldom.newDOMDocument;
node := dbms_xmldom.makeNode(doc);
dbms_xmldom.setversion(doc, '1.0');
dbms_xmldom.setCharset(doc, 'UTF8');
elem := dbms_xmldom.createElement(doc, 'PartnerInfo');
dbms_xmldom.setAttribute(elem,'xmlns','EMP');
cur_node := dbms_xmldom.appendChild(node, dbms_xmldom.makeNode(elem));
mode_elmn := dbms_xmldom.createElement(doc, 'EMPLOYEE');
mode_node := dbms_xmldom.appendChild(cur_node,dbms_xmldom.makeNode(mode_elmn));
BEGIN
SELECT value(e) INTO l_xmltype
FROM TABLE(XMLSequence(Cursor(SELECT * FROM EMP1 where EMPNO=7501))) e;
child_document := DBMS_XMLDOM.newDOMDocument(l_xmltype);
root_node_data1 := dbms_xmldom.importNode(doc,dbms_xmldom.makeNode(dbms_xmldom.getDocumentElement(child_document)),TRUE);
root_node_data1 := DBMS_XMLDOM.appendChild(root_node_data, root_node_data1);
EXCEPTION
WHEN OTHERS THEN
Dbms_Output.Put_Line('Error in SELECT stmt(UC_PARTNER_MS):::'||'error::'||SQLERRM);
END;
dbms_lob.createtemporary(v_doc, true);
dbms_xmldom.writeToClob(doc,v_doc,'UTF8');
v_EMP:= v_doc;
dbms_xmldom.writeToFile(DOC,v_output_filename,'UTF8');
dbms_xmldom.freeDocument(doc);
--Dbms_Output.Put_Line('THE OUTPUT IS::'||V_EMP);
EXCEPTION
WHEN OTHERS THEN
Dbms_Output.Put_Line('Error in SELECT stmt(UC_PARTNER_MS):::'||'error::'||SQLERRM);
END;
The xml file is 'EMP_XML_FILE.xml'
<empno>U++kYmcVuGchxbh+++++++++++++++1+</empno>
<empname>J</empname>
suppose the empno 7501 is not available in our emp table,
i got error
ORA-03113: end-of-file on communication channel
how to handle xmldom with no data values.
by
sivahi,
please give the solution
by
siva -
Writetofile error -- SYS.UTL_FILE
I get below error when I use "dbms_xmldom.writetofile(doc,'output.xml')"
ORA-04067: not executed, package body "SYS.UTL_FILE" does not exist
Is this because the read-from and write-to directories in the initialization.ora file hasn't been specified? Thanks.Yes, and permissions to use UTL_FILE have been tightened up.
-
Reg :DBMS_XMLDOM.DOMDocument Error
Hi all,
I tried to create xml document in the C Drive .
I Created Directory like :
create or replace directory ctemp1 as 'C:\';
GRANT ALL ON DIRECTORY ctemp1 to public;
grant read,write on directory ctemp1 to public;
The code is :
DECLARE
doc DBMS_XMLDOM.DOMDocument;
xdata XMLTYPE;
cursor XMLCUR is select SYS_XMLGEN(XMLAGG(XMLELEMENT("EMPNO", ename))) FROM EMP;
Cursor xmlCurdate is
select SYS_XMLGen(
XMLELEMENT(NAME "UTIMEID",
XMLATTRIBUTES (to_char((sysdate - 1),'yyyymmdd') as "uid",
to_char((sysdate - 1),'mm/dd/yyyy (') || rtrim(to_char((sysdate - 1),'Day')) || ')' as "uiddesc")),
sys.xmlgenformatType.createFormat('UPDATED-XML') ) from dual;
BEGIN
OPEN xmlcur;
FETCH xmlcur INTO xdata;
CLOSE xmlcur;
doc := DBMS_XMLDOM.NewDOMDocument(xdata);
DBMS_XMLDOM.WRITETOFILE(doc, 'ctemp1'||'\'||'alert.xml');
OPEN xmlcurdate;
FETCH xmlcurdate INTO xdata;
CLOSE xmlcurdate;
doc := DBMS_XMLDOM.NewDOMDocument(xdata);
DBMS_XMLDOM.WRITETOFILE(doc, 'ctemp1'||'\'||'updated.xml');
END;
Am Getting the Error like :
Error starting at line 8 in command:
DECLARE
doc DBMS_XMLDOM.DOMDocument;
xdata XMLTYPE;
cursor XMLCUR is select SYS_XMLGEN(XMLAGG(XMLELEMENT("EMPNO", ename))) FROM EMP;
Cursor xmlCurdate is
select SYS_XMLGen(
XMLELEMENT(NAME "UTIMEID",
XMLATTRIBUTES (to_char((sysdate - 1),'yyyymmdd') as "uid",
to_char((sysdate - 1),'mm/dd/yyyy (') || rtrim(to_char((sysdate - 1),'Day')) || ')' as "uiddesc")),
sys.xmlgenformatType.createFormat('UPDATED-XML') ) from dual;
BEGIN
OPEN xmlcur;
FETCH xmlcur INTO xdata;
CLOSE xmlcur;
doc := DBMS_XMLDOM.NewDOMDocument(xdata);
DBMS_XMLDOM.WRITETOFILE(doc, 'ctemp1'||'\'||'alert.xml');
OPEN xmlcurdate;
FETCH xmlcurdate INTO xdata;
CLOSE xmlcurdate;
doc := DBMS_XMLDOM.NewDOMDocument(xdata);
DBMS_XMLDOM.WRITETOFILE(doc, 'ctemp1'||'\'||'updated.xml');
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 33
ORA-06512: at "SYS.UTL_FILE", line 436
ORA-06512: at "XDB.DBMS_XSLPROCESSOR", line 217
ORA-29280: invalid directory path
ORA-29280: invalid directory path
ORA-06512: at "XDB.DBMS_XMLDOM", line 4416
ORA-06512: at line 16
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
Please help on this .
Thanks in advence .When you create objects on the database (tables, views, directories etc.) then by default they are stored in the data dictionary in UPPER case (unless you surround the name with double quotes which is poor practice).
So, in your DBMS_XMLDOM.WRITETOFILE call you need to specify the directory name in UPPER case...
DBMS_XMLDOM.WRITETOFILE(doc, 'CTEMP1'||...... simple as that. ;) -
XML Version/encoding line not showing up
After a long process of getting dbms_xmldom to properly work on 9.2.0.6.0 I generated my output and noticed that the following line is now missing. It was showing up when using just xmldom, but I was running into other issues with attributes showing up on unintended tags.
<?xml version = '1.0' encoding = 'ISO-8859-1'?>Is the tag above generally a required piece? Either way, does anyone have any ideas on how to get it back?
I put the following test code together that illustrates the issue. If I change to just xmldom and specify the file path I get incorrect "Incident" element but the xml version line shows up.
declare
doc dbms_xmldom.DOMDocument;
main_node dbms_xmldom.DOMNode;
root_node dbms_xmldom.DOMNode;
incident_node dbms_xmldom.DOMNode;
item_node dbms_xmldom.DOMNode;
root_elmt dbms_xmldom.DOMElement;
item_elmt dbms_xmldom.DOMElement;
item_text dbms_xmldom.DOMText;
v_clob CLOB := 'a';
v_xml VARCHAR2(32767);
output utl_file.file_type;
begin
-- get document
doc := dbms_xmldom.newDOMDocument;
-- create root element
main_node := dbms_xmldom.makeNode(doc);
root_elmt := dbms_xmldom.createElement(doc, 'Incidents');
dbms_xmldom.setAttribute(root_elmt, 'xmlns','http://www.claimsuite.com'); -- without the :
dbms_xmldom.setAttribute(root_elmt, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
dbms_xmldom.setAttribute(root_elmt, 'xsi:schemaLocation', 'http://www.claimsuite.com ClaimsuiteShellUS.xsd');
root_node := dbms_xmldom.appendChild(main_node, dbms_xmldom.makeNode(root_elmt));
for i in 1 .. 2 loop
item_elmt := dbms_xmldom.createElement(doc, 'Incident');
incident_node := dbms_xmldom.appendChild(root_node, dbms_xmldom.makeNode(item_elmt));
--CrawfordHandlingOfficeReference
item_elmt := dbms_xmldom.createElement(doc, 'HandlingOfficeReference');
item_node := dbms_xmldom.appendChild(incident_node, dbms_xmldom.makeNode(item_elmt));
item_text := dbms_xmldom.createTextNode(doc, 'HandlingOfficeRef');
item_node := dbms_xmldom.appendChild(item_node, dbms_xmldom.makeNode(item_text));
end loop;
-- instead of writing to file, it writes to clob
dbms_xmldom.writeToFile(doc, 'IO_DIR_SHL_RSG/test.xml');
-- free resources
dbms_xmldom.freeDocument(doc);
end;Thank you.Hi,
DBMS_XMLDOM.writeToFile has an overloading where you can specify the output character set :
dbms_xmldom.writeToFile(doc, 'IO_DIR_SHL_RSG/test.xml', 'ISO-8859-1'); -
How to export a data as an XML file from oracle data base?
could u pls tell me the step by step procedure for following questions...? how to export a data as an XML file from oracle data base? is it possible? plz tell me itz urgent requirement...
Thankz in advance
BalaSQL> SELECT * FROM v$version;
BANNER
Oracle DATABASE 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE 11.1.0.6.0 Production
TNS FOR 32-bit Windows: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production
5 rows selected.
SQL> CREATE OR REPLACE directory utldata AS 'C:\temp';
Directory created.
SQL> declare
2 doc DBMS_XMLDOM.DOMDocument;
3 xdata XMLTYPE;
4
5 CURSOR xmlcur IS
6 SELECT xmlelement("Employee",XMLAttributes('http://www.w3.org/2001/XMLSchema' AS "xmlns:xsi",
7 'http://www.oracle.com/Employee.xsd' AS "xsi:nonamespaceSchemaLocation")
8 ,xmlelement("EmployeeNumber",e.empno)
9 ,xmlelement("EmployeeName",e.ename)
10 ,xmlelement("Department",xmlelement("DepartmentName",d.dname)
11 ,xmlelement("Location",d.loc)
12 )
13 )
14 FROM emp e
15 , dept d
16 WHERE e.DEPTNO=d.DEPTNO;
17
18 begin
19 OPEN xmlcur;
20 FETCH xmlcur INTO xdata;
21 CLOSE xmlcur;
22 doc := DBMS_XMLDOM.NewDOMDocument(xdata);
23 DBMS_XMLDOM.WRITETOFILE(doc, 'UTLDATA/marco.xml');
24 end;
25 /
PL/SQL procedure successfully completed.
. -
How to get UTF-8 encoding when create XML using DBMS_XMLGEN and UTL_FILE ?
How to get UTF-8 encoding when create XML using DBMS_XMLGEN and UTL_FILE ?
Hi,
I do generate XML-Files by using DBMS_XMLGEN with output by UTL_FILE
but it seems, the xml-Datafile I get on end is not really UTF-8 encoding
( f.ex. cannot verifying it correct in xmlspy )
my dbms is
NLS_CHARACTERSET = WE8MSWIN1252
NLS_NCHAR_CHARACTERSET = AL16UTF16
NLS_RDBMS_VERSION = 10.2.0.1.0
I do generate it in this matter :
declare
xmldoc CLOB;
ctx number ;
utl_file.file_type;
begin
-- generate fom xml-view :
ctx := DBMS_XMLGEN.newContext('select xml from xml_View');
DBMS_XMLGEN.setRowSetTag(ctx, null);
DBMS_XMLGEN.setRowTag(ctx, null );
DBMS_XMLGEN.SETCONVERTSPECIALCHARS(ctx,TRUE);
-- create xml-file:
xmldoc := DBMS_XMLGEN.getXML(ctx);
-- put data to host-file:
vblob_len := DBMS_LOB.getlength(xmldoc);
DBMS_LOB.READ (xmldoc, vblob_len, 1, vBuffer);
bHandle := utl_file.fopen(vPATH,vFileName,'W',32767);
UTL_FILE.put_line(bHandle, vbuffer, FALSE);
UTL_FILE.fclose(bHandle);
end ;
maybe while work UTL_FILE there is a change the encoding ?
How can this solved ?
Thank you
Norbert
Edited by: astramare on Feb 11, 2009 12:39 PM with database charsetsMarco,
I tryed to work with dbms_xslprocessor.clob2file,
that works good,
but what is in this matter with encoding UTF-8 ?
in my understandig, the xmltyp created should be UTF8 (16),
but when open the xml-file in xmlSpy as UTF-8,
it is not well ( german caracter like Ä, Ö .. ):
my dbms is
NLS_CHARACTERSET = WE8MSWIN1252
NLS_NCHAR_CHARACTERSET = AL16UTF16
NLS_RDBMS_VERSION = 10.2.0.1.0
-- test:
create table nh_test ( s0 number, s1 varchar2(20) ) ;
insert into nh_test (select 1,'hallo' from dual );
insert into nh_test (select 2,'straße' from dual );
insert into nh_test (select 3,'mäckie' from dual );
insert into nh_test (select 4,'euro_€' from dual );
commit;
select * from nh_test ;
S0 S1
1 hallo
1 hallo
2 straße
3 mäckie
4 euro_€
declare
rc sys_refcursor;
begin
open rc FOR SELECT * FROM ( SELECT s0,s1 from nh_test );
dbms_xslprocessor.clob2file( xmltype( rc ).getclobval( ) , 'XML_EXPORT_DIR','my_xml_file.xml');
end;
( its the same when using output with DBMS_XMLDOM.WRITETOFILE )
open in xmlSpy is:
<?xml version="1.0"?>
<ROWSET>
<ROW>
<S0>1</S0>
<S1>hallo</S1>
</ROW>
<ROW>
<S0>2</S0>
<S1>straޥ</S1>
</ROW>
<ROW>
<S0>3</S0>
<S1>m㢫ie</S1>
</ROW>
<ROW>
<S0>4</S0>
<S1>euro_</S1>
</ROW>
</ROWSET>
regards
Norbert -
Query o/p directly written intofile present disk in 11g
in 11g i came to know that xml data from table is directly saved in dsik using below code
Saving XML data directly to disk
The below example explains the how to create xml file and save that file in disk
(1) Create logical directory
CREATE OR REPLACE directory utldata as 'C:\temp';
(2)
declare
doc DBMS_XMLDOM.DOMDocument;
xdata XMLTYPE;
CURSOR xmlcur IS
SELECT xmlelement("Employee",XMLAttributes('http://www.w3.org/2001/XMLSchema' AS
"xmlns:xsi",
'http://www.oracle.com/Employee.xsd' AS
"xsi:nonamespaceSchemaLocation")
,xmlelement("EmployeeNumber",e.empno)
,xmlelement("EmployeeName",e.ename)
,xmlelement("Department",xmlelement("DepartmentName",d.dname)
,xmlelement("Location",d.loc)
FROM emp e
, dept d
WHERE e.DEPTNO=d.DEPTNO;
begin
OPEN xmlcur;
FETCH xmlcur INTO xdata;
CLOSE xmlcur;
doc := DBMS_XMLDOM.NewDOMDocument(xdata);
DBMS_XMLDOM.WRITETOFILE(doc, 'UTLDATA/marco.xml');
end;
The file with name as marco.xml is created and saved in this folder C:\temp
The content of file is shown below
- <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:nonamespaceSchemaLocation="http://www.oracle.com/Employee.xsd">
<EmployeeNumber>7369</EmployeeNumber>
<EmployeeName>SMITH</EmployeeName>
- <Department>
<DepartmentName>RESEARCH</DepartmentName>
<Location>DALLAS</Location>
</Department>
</Employee>
similarly is there any method for o/p of the query reslut directly written into file which is in disk in 11gThere are various ways, but generally you would use UTL_FILE to write out data to a file:
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
,p_dir IN VARCHAR2
,p_header_file IN VARCHAR2
,p_data_file IN VARCHAR2 := NULL) IS
v_finaltxt VARCHAR2(4000);
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
v_fh UTL_FILE.FILE_TYPE;
v_samefile BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
d := DBMS_SQL.EXECUTE(c);
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
END CASE;
END LOOP;
-- This part outputs the HEADER
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
FOR j in 1..col_cnt
LOOP
v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
IF NOT v_samefile THEN
UTL_FILE.FCLOSE(v_fh);
END IF;
-- This part outputs the DATA
IF NOT v_samefile THEN
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
END IF;
LOOP
v_ret := DBMS_SQL.FETCH_ROWS(c);
EXIT WHEN v_ret = 0;
v_finaltxt := NULL;
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
ELSE
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
END CASE;
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
END LOOP;
UTL_FILE.FCLOSE(v_fh);
DBMS_SQL.CLOSE_CURSOR(c);
END;This allows for the header row and the data to be written to seperate files if required.
e.g.
SQL> exec run_query('select * from emp','TEST_DIR','output.txt');
PL/SQL procedure successfully completed.Output.txt file contains:
empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10 -
Hi all
I want a sample procedure in which i would to like to fetch data and save the output to a XML File.
Someone please help me in giving me the sample procedure.
Regds
Nirmalsee this link, may be it will help
http://blog.oraclecontractors.com/?p=147
Well that shows you how to extract data on the database as XML, but it doesn't show how to output it to a file.
I'll give a hint. If you have your data as an XMLTYPE you can convert it via CLOB to and XMLDOMDocument type (as in the DBMS_XMLDOM package). Once you have an XMLDOMDocument you can use one of the DBMS_XMLDOM procedures to write out the XML to a file...
DBMS_XMLDOM.WRITETOFILE(
doc IN DOMDOCUMENT,
filename IN VARCHAR2);
;) -
Formatting XML using DBMS_XMLGEN
Hi.
Having Problems with generating XML out of a query using nested table types
-- Heres a test case
drop type test_tab
drop type test_type
drop type sub_type_tab
drop type sub_type
create or replace type sub_type as object ( "@a" varchar2(10), "@b" varchar2(10) );
create or replace type sub_type_tab as table of sub_type;
create or replace type test_type as object ( "@c" VARCHAR2(10), test sub_type_tab );
CREATE OR REPLACE TYPE test_tab AS TABLE OF test_type;
CREATE OR REPLACE VIEW test_view OF test_type
WITH OBJECT IDENTIFIER ("@c")
AS
SELECT
'C',
CAST(MULTISET(SELECT 'A', 'B' FROM DUAL) AS sub_type_tab)
FROM dual
The resultant XML out of DBMS_XMLGEN.GETXML is :
<?xml version="1.0"?>
<ROWSET>
<ROW c = "C">
<TEST>
<SUB_TYPE a="A" b="B"/>
</TEST>
</ROW>
</ROWSET>
As you can see as a consequence of the column collection type alias in the view
i have an extraneous <TEST> tag.
Does anyone know how I can remove this tag entry?
Thanks in advanceMarco,
I tryed to work with dbms_xslprocessor.clob2file,
that works good,
but what is in this matter with encoding UTF-8 ?
in my understandig, the xmltyp created should be UTF8 (16),
but when open the xml-file in xmlSpy as UTF-8,
it is not well ( german caracter like Ä, Ö .. ):
my dbms is
NLS_CHARACTERSET = WE8MSWIN1252
NLS_NCHAR_CHARACTERSET = AL16UTF16
NLS_RDBMS_VERSION = 10.2.0.1.0
-- test:
create table nh_test ( s0 number, s1 varchar2(20) ) ;
insert into nh_test (select 1,'hallo' from dual );
insert into nh_test (select 2,'straße' from dual );
insert into nh_test (select 3,'mäckie' from dual );
insert into nh_test (select 4,'euro_€' from dual );
commit;
select * from nh_test ;
S0 S1
1 hallo
1 hallo
2 straße
3 mäckie
4 euro_€
declare
rc sys_refcursor;
begin
open rc FOR SELECT * FROM ( SELECT s0,s1 from nh_test );
dbms_xslprocessor.clob2file( xmltype( rc ).getclobval( ) , 'XML_EXPORT_DIR','my_xml_file.xml');
end;
( its the same when using output with DBMS_XMLDOM.WRITETOFILE )
open in xmlSpy is:
<?xml version="1.0"?>
<ROWSET>
<ROW>
<S0>1</S0>
<S1>hallo</S1>
</ROW>
<ROW>
<S0>2</S0>
<S1>straޥ</S1>
</ROW>
<ROW>
<S0>3</S0>
<S1>m㢫ie</S1>
</ROW>
<ROW>
<S0>4</S0>
<S1>euro_</S1>
</ROW>
</ROWSET>
regards
Norbert -
Changing the encoding attribute in xml output
We are creating an xml file using:
vc_string_clob := dbms_xmlgen.getxml('select * from mytable');
then opening the file with UTL_FILE.FOPEN('dir','filename','W',31767)
then parsing through vc_string_clob and using UTL_FILE.PUT_LINE to write to the file.
This all works fine, expect the header of the XML file is <?xml version="1.0"?> and our users need it to be <?xml version="1.0" encoding="ISO-8859-1"?>
Is there a function within dbms_xmlgen that will allow me to change this?
I'm thinking I can use brute force to change it, but I'm sure there is a 'more correct' way to go about it.
Thank you,
Jill JacomineHi Jill,
Welcome to the forum.
There are easier methods to output XML to a file, rather than using line-by-line UTL_FILE operations.
For example :
DECLARE
xml_result xmltype;
doc dbms_xmldom.DOMDocument;
BEGIN
xml_result := dbms_xmlgen.getXMLType('SELECT * FROM scott.dept');
doc := dbms_xmldom.newDOMDocument(xml_result);
dbms_xmldom.writeToFile(doc, 'TEST_DIR/my_file.xml', 'ISO-8859-1');
dbms_xmldom.freeDocument(doc);
END;
/or,
DECLARE
xml_result xmltype;
xml_prolog clob := '<?xml version="1.0" encoding="ISO-8859-1"?>'||chr(10);
BEGIN
xml_result := dbms_xmlgen.getXMLType('SELECT * FROM scott.dept');
dbms_xslprocessor.clob2file(xml_prolog || xml_result.getClobVal(), 'TEST_DIR', 'my_file.xml');
END;
/Both will produce :
<?xml version="1.0" encoding="ISO-8859-1"?>
<ROWSET>
<ROW>
<DEPTNO>10</DEPTNO>
<DNAME>ACCOUNTING</DNAME>
<LOC>NEW YORK</LOC>
</ROW>
<ROW>
<DEPTNO>20</DEPTNO>
<DNAME>RESEARCH</DNAME>
<LOC>DALLAS</LOC>
</ROW>
<ROW>
<DEPTNO>30</DEPTNO>
<DNAME>SALES</DNAME>
<LOC>CHICAGO</LOC>
</ROW>
<ROW>
<DEPTNO>40</DEPTNO>
<DNAME>OPERATIONS</DNAME>
<LOC>BOSTON</LOC>
</ROW>
</ROWSET>Hope that helps. -
Write XML to file - character set problem
I have a package that generates XML from relational data using SQLX. I want to write the resulting XML to the unix file system. I can do this with the following code :
DECLARE
v_xml xmltype;
doc dbms_xmldom.DOMDocument;
BEGIN
v_xml := create_my_xml; -- returns an XMLType value
doc := dbms_xmldom.newDOMDocument(v_xml);
dbms_xmldom.writeToFILE(doc, '/mydirectory/myfile.xml');
END;
This creates the file but characters such å,ä and ö are getting 'corrupted' and the resultant xml is invalid.(I've checked the xml within SQL*Plus and the characters are OK)
I assume the character set of the unix operating system doesn't support these characters. How can I overcome this ?Hi,
Do you mean that you would like to write output to an external file somewhere on flask disk or perhaps even inside the directory where the MIDlet is located?? To be able to do so you will need manufacturere specific APIs extended on FileConnection (=JSR, don't know the number right now...). The default MIDP I/O libary does not support direct action on a file.
However, such a FileConnection method invocation requires an import statement that is manufacturere specifc...
To keep your MIDlet CLDC MIDP compliant you can try using RMS with which you can write data that will be stored in a 'database' within the 'res' directory inside your MIDlet suite. If you're new to RMS, please check the web for tutorials, etc etc.
Cheers for now,
Jasper -
Hi, I wrote a package in Oracle 9i (9.2.0.6.0) that utilizes the logic shown below.
declare
doc dbms_xmldom.DOMDocument;
main_node dbms_xmldom.DOMNode;
root_node dbms_xmldom.DOMNode;
incident_node dbms_xmldom.DOMNode;
item_node dbms_xmldom.DOMNode;
root_elmt dbms_xmldom.DOMElement;
item_elmt dbms_xmldom.DOMElement;
item_text dbms_xmldom.DOMText;
v_clob CLOB := 'a';
v_xml VARCHAR2(32767);
output utl_file.file_type;
begin
-- get document
doc := dbms_xmldom.newDOMDocument;
-- create root element
main_node := dbms_xmldom.makeNode(doc);
root_elmt := dbms_xmldom.createElement(doc, 'Incidents');
dbms_xmldom.setAttribute(root_elmt, 'xmlns','http://www.claimsuite.com');
dbms_xmldom.setAttribute(root_elmt, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
dbms_xmldom.setAttribute(root_elmt, 'xsi:schemaLocation', 'http://www.claimsuite.com ClaimsuiteShellUS.xsd');
root_node := dbms_xmldom.appendChild(main_node, dbms_xmldom.makeNode(root_elmt));
for i in 1 .. 2 loop
item_elmt := dbms_xmldom.createElement(doc, 'Incident');
incident_node := dbms_xmldom.appendChild(root_node, dbms_xmldom.makeNode(item_elmt));
--CrawfordHandlingOfficeReference
item_elmt := dbms_xmldom.createElement(doc, 'HandlingOfficeReference');
item_node := dbms_xmldom.appendChild(incident_node, dbms_xmldom.makeNode(item_elmt));
item_text := dbms_xmldom.createTextNode(doc, 'HandlingOfficeRef');
item_node := dbms_xmldom.appendChild(item_node, dbms_xmldom.makeNode(item_text));
end loop;
-- instead of writing to file, it writes to clob
dbms_xmldom.writeToClob(doc,v_clob);
v_clob := '<?xml version="1.0" encoding="ISO-8859-1"?>'||chr(10)||v_clob;
DBMS_XSLPROCESSOR.clob2file(v_clob,'IO_DIR_SHL_RSG','test1.xml',0);
--dbms_xmldom.writeToFile(doc, 'IO_DIR_SHL_RSG/test.xml','ISO-8859-1');
-- free resources
dbms_xmldom.freeDocument(doc);
end;It works great but is a huge memory hog. The xml tag structure goes way deeper that the example above but I was wondering if anyone has any tips on the code methodology I'm use or has any suggestions to improve the speed. I'm working with a database of about 10,000 records in it.
Thank you.I did consider that at first, but my PL/SQL code got so complex that I had to revert to the xmldom approach. For example, I had to omit certain elements when data was null, pass parameters to function etc.....Here is a snippet
open payments_cur(claim_rec.claim_id);
fetch payments_cur into payRec;
if payments_cur%FOUND then
item_elmt := dbms_xmldom.createElement(doc, 'Payments');
payments_node := dbms_xmldom.appendChild(claim_node, dbms_xmldom.makeNode(item_elmt));
else
null;
end if;
close payments_cur;
for pay_rec in payments_cur(claim_rec.claim_id) loop
--payment
item_elmt := dbms_xmldom.createElement(doc, 'Payment');
payment_node := dbms_xmldom.appendChild(payments_node, dbms_xmldom.makeNode(item_elmt));
--PaymentbyCheque
create_xml_element('PaymentbyCheque',payment_node,pay_rec.PaymentbyCheque,true,1);
--PaymentAmount
create_xml_element('PaymentAmount',payment_node,pay_rec.PaymentAmount,true);
--LocalTaxAmount
create_xml_element('LocalTaxAmount',payment_node,pay_rec.LocalTaxAmount);
Maybe you are looking for
-
Servlet session objects shariing same session objects? Help!
Hi all, Is it possible for two different HttpSessions to share the same session objects. We have a java class that we store in the session. This class calls a stateless Session EJB to do quoting for an insurance policy. It seems t
-
Hi Friends, There are around 25 aggregates on a cube. When rollup was done today, it filled and activated 24 aggregates. One aggregate is showing red status and switched off. When I am trying to activate it manually, it is not activating. Could anybo
-
How to create a table which have always only one entry inside it?
Hi Experts, Is it possible to create a table in data dictionary which have always only one entry inside it. If we insert another entry, then it should throw an error.
-
Create smart playlist - "shuffle randomly" - burn DVD
The above is all what I want to do. I can create a smart playlist no problem. I can burn a DVD with the playlist no problem. BUT I can not get the playlist to randomly shuffled in the itunes window before I burn the disk?!! Shuffle is on "By Song" an
-
I'm trying to upload my iMovie to facebook for school just in case it doesn't work from on my USB, but I don't want to upload it to youtube. I was just wondering what that setting did . . .