Skip to Main Content
  • Questions
  • How to extract Oracle data into a XML formatted file

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Rene.

Asked: December 20, 2002 - 4:19 pm UTC

Last updated: February 14, 2005 - 8:37 am UTC

Version: 9.2.0

Viewed 1000+ times

You Asked

Hi Tom,

On our project, we need to extract data from our Oracle9i database to generate a XML formatted file, and use the XML file as portable media then import data from the XML data file to either a MS SQL Server database or an Oracle database on our client site. Is there an Oracle utility/tool available to accomplish the task? Any idea or advice would be greatly appreciated.

Thanks in advance and happy holiday!

Rene Zhao

DFI International


and Tom said...

You mean like this:

ops$tkyte@ORA920> select dbms_xmlgen.getxml( 'select * from scott.emp' ) from dual;

DBMS_XMLGEN.GETXML('SELECT*FROMSCOTT.EMP')
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
<ROW>
<EMPNO>7369</EMPNO>
<ENAME>SMITH</ENAME>
<JOB>CLERK</JOB>
<MGR>7902</MGR>
<HIREDATE>17-DEC-80</HIREDATE>
<SAL>800</SAL>
<DEPTNO>20</DEPTNO>
</ROW>
<ROW>
<EMPNO>7499</EMPNO>
<ENAME>ALLEN</ENAME>
<JOB>SALESMAN</JOB>
<MGR>7698</MGR>
<HIREDATE>20-FEB-81</HIREDATE>
<SAL>1600</SAL>
<COMM>300</COMM>
<DEPTNO>30</DEPTNO>
</ROW>
......


You want to read:

</code> http://docs.oracle.com/cd/B10501_01/nav/docindex.htm#index-XML <code>

the xml developers guide -- you'll find the XML support to be pretty comprehensive....




Rating

  (12 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

XML generation in Oracle8i

Anil, December 21, 2002 - 5:47 am UTC

Hi Tom:
I tried using the xmlgen package in 8i but it failed with this error. Can you please give me a list of steps(checklist) which I need to check. I donot want to blindly increase the size of my shared pool. I just want to findout why this has occured - so I need your help it actually finding the source of the problem.

select xmlgen.getxml('select * from tags') from dual
*
ERROR at line 1:
ORA-04031: unable to allocate 4032 bytes of shared memory ("shared
pool","OracleXMLStoreSYS","joxlod: in ehe","ioc_allocate_pal")
ORA-06512: at "SYS.XMLGEN", line 487
ORA-06512: at "SYS.XMLGEN", line 477
ORA-06512: at line 1

Thanks for your time.

Best Regards,
Anil

Tom Kyte
December 21, 2002 - 9:01 am UTC

make sure your java pool and shared pool are set large enough (they are not right now)

VINI, June 27, 2003 - 10:58 am UTC

Hi,

I too have a ORA-04031.

I did the "alter system flush shared pool". but still has the problem.

our people doesn't want to change existing sql (for bind variables), and or shutdown the database.

so my problem is :
Is there any other way to refresh the shared memory ?



Tom Kyte
June 27, 2003 - 12:08 pm UTC

how big is your shared pool

and why won't the developers fix the bugs in their code? make fun of them, maybe they'll get it.


have you tried cursor_sharing?

ORA-04031: unable to allocate 4032 bytes of shared memory ("shared

Yong, June 27, 2003 - 1:22 pm UTC

I encoutered the similar problem on Oracle8i before. The real problem was that the pl/sql package was locked. For example, the package is running and you try to re-compile the package from other tool, then you will get this error. You may find the process to kill the compiling process. You may not be able drop/re-create the package. There is a undocument way to break the lock.



Tom Kyte
June 27, 2003 - 1:39 pm UTC

umm, no, that won't do that. you'll get a timeout, not an "out of memory"

CHop off

A reader, June 27, 2003 - 3:13 pm UTC

Hi, Tom,

I tried you code, but only got some of the output, the others are chopped of:
DBMS_XMLGEN.GETXML('SELECT*FROMSCOTT.EMP')
------------------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
<ROW>
<EMPNO>7369</EMPNO>
<ENAME>SMITH</ENAM
<--------Missing parts
1 row selected.
Only one partial row? Or do I need to do somthing here?

How to get XML data back to the DB?

THanks

Tom Kyte
June 27, 2003 - 3:49 pm UTC

SQL> set long 500000

or some big number.  just a sqlplus setting.

search for

xmlsave

on this site. 

how can we insert data into oracle from XML file

A Reader, July 28, 2003 - 6:30 am UTC

hi Tom ...
need ur help Again ...

how can we insert data into oracle from XML file

Thanx

Re:

a reader, July 29, 2003 - 3:20 am UTC

hii ... Tom ..

we are using 8.1.5 ...


Tom Kyte
July 29, 2003 - 7:08 am UTC

see otn.oracle.com, goto the xml technology corner. much of the stuff can be loaded into 8i, some of it must run outside of the database.

Why null in fields?

Sasa, August 18, 2003 - 6:54 am UTC

Hi Tom,

I created test case(hope will help).

(it is Oracle 9.2.03)

create table xmltest (id number, xmlpayload xmltype);

Table created.

insert into xmltest(id, xmlpayload)
values(1, xmltype(
'<?xml version="1.0"?>
<ROWSET>
<ROW>
<IDFOBETSTATE>A</IDFOBETSTATE>
<ISACTIVE>-1</ISACTIVE>
<INTERNALORDER>1</INTERNALORDER>
<INTERNALDESCRIPTION>Bet attempted (not open/valid)</INTERNALDESCRIPTION>
<NAME>Attempted</NAME>
</ROW>
<ROW>
<IDFOBETSTATE>O</IDFOBETSTATE>
<ISACTIVE>-1</ISACTIVE>
<INTERNALORDER>2</INTERNALORDER>
<INTERNALDESCRIPTION>Bet opened</INTERNALDESCRIPTION>
<NAME>Open</NAME>
</ROW>
<ROW>
<IDFOBETSTATE>C</IDFOBETSTATE>
<ISACTIVE>-1</ISACTIVE>
<INTERNALORDER>3</INTERNALORDER>
<INTERNALDESCRIPTION>Bet closed(All legs closed - not settled)</INTERNALDESCRIPTION>
<NAME>Closed</NAME>
</ROW>
<ROW>
<IDFOBETSTATE>S</IDFOBETSTATE>
<ISACTIVE>-1</ISACTIVE>
<INTERNALORDER>4</INTERNALORDER>
<INTERNALDESCRIPTION>Bet settled(Bet is paid out)</INTERNALDESCRIPTION>
<NAME>Settled</NAME>
</ROW>
<ROW>
<IDFOBETSTATE>?</IDFOBETSTATE>
<ISACTIVE>-1</ISACTIVE>
<INTERNALORDER>5</INTERNALORDER>
<INTERNALDESCRIPTION>Bet undefined(whatever it means)</INTERNALDESCRIPTION>
<NAME>Unknown</NAME>
</ROW>
</ROWSET>'));

1 row created.

commit;

Commit complete.

SELECT
extract(value(d),'ROWSET/ROW/IDFOBETSTATE/text()').getstringVal() as IDFOBETSTATE,
extract(value(d),'ROWSET/ROW/INTERNALORDER/text()').getnumberVal() as INTERNALORDER
FROM xmltest t,
TABLE(XMLSequence(t.xmlpayload.extract('ROWSET/ROW'))) d;


This query shows me correctly 5 records but with the null in the fields.

What am I missing?

Please, help.


Tom Kyte
August 18, 2003 - 7:31 am UTC

because the rowset is gone by then... always helpful to just dump the query out and see what it is giving you back.  I started with "select *" but backed off to just select column_value for showing you what is there:


ops$tkyte@ORA920> SELECT         column_value
  2  FROM xmltest t,
  3  TABLE(XMLSequence(t.xmlpayload.extract('ROWSET/ROW'))) d;

COLUMN_VALUE
-----------------------------------------------------------------------------------------------------------------------------------
<ROW>
  <IDFOBETSTATE>A</IDFOBETSTATE>
  <ISACTIVE>-1</ISACTIVE>
  <INTERNALORDER>1</INTERNALORDER>
  <INTERNALDESCRIPTION>Bet attempted (not open/valid)</INTERNALDESCRIPTION>
  <NAME>Attempted</NAME>
</ROW>

<ROW>
  <IDFOBETSTATE>O</IDFOBETSTATE>
  <ISACTIVE>-1</ISACTIVE>
  <INTERNALORDER>2</INTERNALORDER>
  <INTERNALDESCRIPTION>Bet opened</INTERNALDESCRIPTION>
  <NAME>Open</NAME>
</ROW>

<ROW>
  <IDFOBETSTATE>C</IDFOBETSTATE>
  <ISACTIVE>-1</ISACTIVE>
  <INTERNALORDER>3</INTERNALORDER>
  <INTERNALDESCRIPTION>Bet closed(All legs closed - not settled)</INTERNALDESCRIPTION>
  <NAME>Closed</NAME>
</ROW>

<ROW>
  <IDFOBETSTATE>S</IDFOBETSTATE>
  <ISACTIVE>-1</ISACTIVE>
  <INTERNALORDER>4</INTERNALORDER>
  <INTERNALDESCRIPTION>Bet settled(Bet is paid out)</INTERNALDESCRIPTION>
  <NAME>Settled</NAME>
</ROW>

<ROW>
  <IDFOBETSTATE>?</IDFOBETSTATE>
  <ISACTIVE>-1</ISACTIVE>
  <INTERNALORDER>5</INTERNALORDER>
  <INTERNALDESCRIPTION>Bet undefined(whatever it means)</INTERNALDESCRIPTION>
  <NAME>Unknown</NAME>
</ROW>

<b>so, what you want is:</b>


ops$tkyte@ORA920>
ops$tkyte@ORA920> SELECT
  2  extract(value(d),'/ROW/IDFOBETSTATE/text()').getstringVal() as
  3  IDFOBETSTATE,
  4  extract(value(d),'/ROW/INTERNALORDER/text()').getnumberVal() as
  5  INTERNALORDER
  6  FROM xmltest t,
  7  TABLE(XMLSequence(t.xmlpayload.extract('ROWSET/ROW'))) d;

IDFOBETSTATE    INTERNALORDER
--------------- -------------
A                           1
O                           2
C                           3
S                           4
?                           5

ops$tkyte@ORA920> 

It works now

Sasa, August 18, 2003 - 11:01 am UTC

Thanks Tom,

Now works.

How to extract Oracle data into a XML formatted file, ver 9.0.1

Raj, August 21, 2003 - 5:35 pm UTC

Hi Tom,
I have the following problem....

SQL> select dbms_xmlgen.getxml( 'select * from scott.emp' ) from 
  2  dual;
ERROR:
ORA-01722: invalid number


no rows selected

Could you know what's wrong?

Raj 

Tom Kyte
September 25, 2003 - 10:18 am UTC

I asked Sean Dillon, our local XML guy, to take a look at this and here's what he had to say:
-------

Hiya Raj,

I'm not sure why you're getting this error, but a couple things you can try:

1.  Make sure you have the appropriate SELECT grant on SCOTT.EMP.
2.  Try the same statement but specify the columns instead of "*". For example, "select empno, ename, job from scott.emp".  See what that gives you.
3.  Make sure you can use DBMS_XMLGEN.GETXML on a simple table you create:

SQL> create table t( txt varchar2(50), num number );
Table created.

SQL> insert into t values ( 'One', 1 );
1 row created.

SQL> insert into t values ( 'Two', 2 );
1 row created.

SQL> insert into t values ( 'Three', 3 );
1 row created.

SQL> insert into t values ( 'Four', 4 );
1 row created.

SQL> select dbms_xmlgen.getxml( 'select * from t' )
  2    from dual;

DBMS_XMLGEN.GETXML('SELECT*FROMT')
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
 <ROW>
  <TXT>One</TXT>
  <NUM>1</NUM>
 </ROW>
 <ROW>
  <TXT>Two</TXT>
  <NUM>2</NUM>
 </ROW>
 <ROW>
  <TXT>Three</TXT>
  <NUM>3</NUM>
 </ROW>
 <ROW>
  <TXT>Four</TXT>
  <NUM>4</NUM>
 </ROW>
</ROWSET>

If you have the appropriate privileges (you can just "select * from scott.emp" logged in as the same user...), and the test above works just dandy, let me know. 

CLOB or XMLType

A reader, August 26, 2003 - 9:05 am UTC

Tom,
Which is the better option to store XML data
1)CLOB data type
2)XMLType

When writing(inserting) data to a table, the options I know we have in oracle are
dbms_xmlSave.InsertXML and
dbms_lob.loadfromfile
Which option is the better option to go with?
Thank you

Tom Kyte
August 26, 2003 - 10:35 am UTC

depends on what you want to do with it (as always)

if you want to have xml functionality -- xmltype makes sense.

if it is just a bit bucket, clob might be good enough.

Is SYS_XMLGEN supported in Oracle 9.2.0

Sap, February 11, 2005 - 3:09 am UTC

Hi Tom,
     When i am trying to run a simple query using SYS_XMLGEN, i encountered a small problem, can you help me out with this error.

  1* select SYS_XMLGEN(empno) from asg_emp
SQL> /
ERROR:
ORA-31011: XML parsing failed



    

Tom Kyte
February 11, 2005 - 8:02 pm UTC

care to share perhaps the data you used?

DBMS_XMLGEN or SYS_XMLGEN??

sap, February 14, 2005 - 7:27 am UTC

Hi Tom,
Which is better option Dnms_xmlgen or Sys_xmlgen?
Or, how can i decide which one to use Dbms_xmlgen or Sys_xmlgen?

Tom Kyte
February 14, 2005 - 8:37 am UTC

benchmark, as always.

neither is "better", they are different. used in different places.

More to Explore

DBMS_XMLGEN

More on PL/SQL routine DBMS_XMLGEN here