2010-10-20 9 views
5

Come si lavora con il tipo di dati XML Oracle?Introduzione rapida su come utilizzare oracle xml tipo di dati

Questa domanda è destinato ad essere asked and answered da me, solo per condividere le informazioni con gli altri

+1

BAD JOB. O prendi un blog o fai questo wiki della comunità !!! –

+3

Come si fa a renderlo un "wiki della comunità"? Avevo l'impressione che questo tipo di cose venisse incoraggiato, come da [stackoverflow faq] (http://stackoverflow.com/faq) – corydoras

+0

Tuttavia, dovrebbe comunque essere una domanda. "Come lavoro con" è un po 'ampio. +1 comunque. – Thilo

risposta

8

il seguente SQL esempio illustra come inserire, campi di database di query e di aggiornamento che contengono il tipo di dati XMLTYPE:

-- Create a table that can store XML 
create table sample_xml (id number, xml xmltype); 

-- Insert some XML into the table 
insert into sample_xml values (1, xmltype.createxml('<subject><name>test</name><list><li>a</li><li>b</li></list></subject>')); 
insert into sample_xml values (2, xmltype.createxml('<subject><name>test</name><list><li>a</li></list></subject>')); 

-- When doing a select, refer to table using the alias or getClobVal() will not work 
select t.id, t.xml.getClobVal() from sample_xml t; 

-- Update text of a single xml element 
UPDATE sample_xml SET xml = UPDATEXML(xml, '/subject/name/text()','happy') WHERE id = 2; 

-- Select out just the name for each row in the table using xpath 
select id, extractvalue(xml, '/subject/name/text()') from sample_xml; 

-- Doing an sample_xml, where the xpath string matches two elements, the text of both elements will be updated 
UPDATE sample_xml SET xml = UPDATEXML(xml, '/subject/list/li/text()','one'); 
Problemi correlati