2011-07-08 32 views
1

下面是存儲在Oracle列中的示例XML代碼解析XML代碼,並重定向到表/文件

<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> 

我想通過節點類似

解析文件爲節點此XML代碼和緩衝數據

預計輸出繼電器

Baiying_attr_03,false 
Baiying_attr_04,false 
Baiying_attr_05,false 

其實我得到的

Baiying_attr_03falseBaiying_attr_04falseBaiying_attr_05false 

我試圖在遊標取數據,但我得到所有這些數據在單個事務,從而不能分割。如果我想要得到的輸出如下那麼我應該用什麼

下面是我的代碼

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; 
/

回答

1

這是更接近你想要的輸出?

/* 
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;