parsing xml code and redirecting to table/file
Below is the sample XML Code stored in Oracle column
<productAttribute>
<name>Baiying_attr_03</name>
<required>false</required>
</productAttribute>
<productAttrib开发者_如何学JAVAute>
<name>Baiying_attr_04</name>
<required>false</required>
</productAttribute>
<productAttribute>
<name>Baiying_attr_05</name>
<required>false</required>
</productAttribute>
I want to parse this xml code and spool data in file as node by node something like
Expected Ouput
Baiying_attr_03,false
Baiying_attr_04,false
Baiying_attr_05,false
Actually I am getting as
Baiying_attr_03falseBaiying_attr_04falseBaiying_attr_05false
I tried to fetch data in cursor but I am getting all this data in single transaction so can't split. If I want to get output as below then what should I use
Following was my code
SET serveroutput ON;
DECLARE
data_text VARCHAR2(32765);
CURSOR c1 IS
SELECT XMLTYPE(XMLDATA).EXTRACT('//productAttribute/text()').getStringVal()
FROM TABLE;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO data_text;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(data_text);
END LOOP;
CLOSE c1;
END;
/
Is this closer to the output you want?
/*
NAME REQUIRED
------------------------------ ------------------------------
Baiying_attr_03 false
Baiying_attr_04 false
Baiying_attr_05 false
3 rows selected.
*** Note: I added ROWSET tags around your XML Code *** */
select lines.*
from (select xmltype(
'<ROWSET><productAttribute><name>Baiying_attr_03</name><required>false</required> </productAttribute> <productAttribute> <name>Baiying_attr_04</name> <required>false</required> </productAttribute> <productAttribute> <name>Baiying_attr_05</name> <required>false</required> </productAttribute></ROWSET>') as the_xml
from dual) zz,
xmltable('for $i in /ROWSET/productAttribute
return $i' PASSING
zz.the_xml COLUMNS name varchar2(30) PATH 'name', required varchar2(30) PATH 'required') lines;
精彩评论