Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle 10g: Extract data (select) from XML (CLOB Type)

I'm new in Oracle and I've - maybe trivial - a problem in a select. (I'm using Oracle 10g Express Edition).

I've a DB with a field CLOB: mytab.xml This column have an XML like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<info>
<id> 954 </id>
<idboss> 954 </idboss>
<name> Fausto </name>
<sorname> Anonimo </sorname>
<phone> 040000000 </phone>
<fax> 040000001 </fax>
</info>

I'm trying to do a 'simple' select to get, for example, the value of 'fax' tag. But I've a bit of problem and I'm not able to understand my error. For example:

select extract(xml, '//fax').getStringVal() from mytab;
ORA-00932: inconsistent datatypes: expected - got

select extract(xmltype(xml), '//fax').getStringVal() from mytab;
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.XMLTYPE", line 254

I've tryed also with 'extractvalue', but I've the same problems. where I'm wrong to do this?

like image 941
mymark Avatar asked Feb 03 '11 09:02

mymark


People also ask

How does CLOB store XML?

We will use the XMLTYPE call to cast the text provided into the XMLTYPE datatype. In the background Oracle XMLTYPE supports CLOB datatype, because XML is stored internally as a CLOB. This means we can use the same approach to casting, passing the call to XMLTYPE a string up to 4GB in size.

How do I export from CLOB?

Solution: I export data in XML or PDF or loader or Json. Then i got data that is clob or blob. Tools > Database Export > select connection > uncheck export DDl if you want only data > check export data > select format: XML > next > next > next > next >finish.


2 Answers

Try this instead:

select xmltype(t.xml).extract('//fax/text()').getStringVal() from mytab t
like image 170
dogbane Avatar answered Sep 21 '22 08:09

dogbane


Try using xmltype.createxml(xml).

As in,

select extract(xmltype.createxml(xml), '//fax').getStringVal() from mytab;

It worked for me.

If you want to improve or manipulate even further.

Try something like this.

Select *
from xmltable(xmlnamespaces('some-name-space' as "ns", 
                                  'another-name-space' as "ns1",
                           ), 
                    '/ns/ns1/foo/bar'
                    passing xmltype.createxml(xml) 
                    columns id varchar2(10) path '//ns//ns1/id',
                            idboss varchar2(500) path '//ns0//ns1/idboss',
                            etc....

                    ) nice_xml_table

Hope it helps someone.

like image 43
Silva Avatar answered Sep 18 '22 08:09

Silva