views:

322

answers:

1

Please see PL/SQL snippets below:

create table t23 (
    field01 number, 
    field02 number, 
    field03 char(1) 
);

Example Snippet #1

declare x varchar2(2000) := '
<ArrayOfRecords>
<Record>
<Field01>130</Field01> 
<Field02>1700</Field02> 
<Field03>C</Field03> 
</Record> 
<Record>
<Field01>131</Field01> 
<Field02>1701</Field02> 
<Field03>B</Field03> 
</Record>                         
</ArrayOfRecords>';

begin
    insert
    into    T23
    SELECT  *
    FROM    XMLTABLE('/ArrayOfRecords/Record'
                PASSING xmltype(x)
                    COLUMNS 
                    Field01 number PATH 'Field01',
                    Field02 number PATH 'Field02',
                    Field03 char(1) PATH 'Field03'
            );
end;

-- The records will be here - great!
select  * from T23;

Example Snippet #2

declare y varchar2(2000) := '
<ArrayOfRecords>
<Record Field01="130" Field02="1700" Field03="C" />
<Record Field01="131" Field02="1701" Field03="B" />
</ArrayOfRecords>';

begin
    insert
    into    T23
    SELECT  *
    FROM    XMLTABLE('/ArrayOfRecords/Record'
                PASSING xmltype(y)
                    COLUMNS 
                    Field01 number PATH 'Field01',
                    Field02 number PATH 'Field02',
                    Field03 char(1) PATH 'Field03'
            );
end;

-- null values are inserted into the table - not great
select  * from T23;

How do you get the values out of the XML attributes in example snippet #2? Is there a way to tell xmltype to retrieve data from attributes vs. elements?

+2  A: 

Use the @ syntax for attributes

select *
  from xmltable('/ArrayOfRecords/Record'
         passing xmltype(y)
         columns
         field01 number path '@Field01',
         field02 number path '@Field02',
         field03 char(1) path '@Field03'
       );
Dougman