2014-06-18 57 views
0

我有一個列表,裏面有一個列表類型屬性(地址)。我發送整個信息作爲數據庫中的XML來更新/插入我的oracle表中的細節。解析存儲過程中的xml oracle

這裏是XML格式

 DECLARE 
    x XMLType := XMLType(' 

    <person> 
    <row>  
    <name>Tom</name>  
    <Address>   
    <LocalAddress>   
    <State>California</State>   
    <City>Los angeles</City>   
    </LocalAddress>   
    <LocalAddress>   
    <State>California1</State>   
    <City>Los angeles1</City>   
    </LocalAddress>  
    </Address> 
    </row> 
    <row>  
    <name>Jim</name>  
    <Address>  
    <LocalAddress> 
    <State>California</State>   
    <City>Los angeles</City>  
    </LocalAddress> 
    </Address> 
    </row> 
    </person>'); 
    v_person_name varchar2(1000); 
    v_city varchar2(1000); 
    BEGIN 
     FOR xmlrow IN 
     (SELECT column_value AS xml 
     FROM TABLE(XMLSequence(x.extract('/person/row'))) 
    ) 
    LOOP 
     SELECT extractValue(xmlrow,'/row/name/text()') 
     INTO v_person_name 
     FROM dual; 
     DBMS_OUTPUT.put_line(v_person_name); 
     FOR address IN 
     (SELECT column_value AS xml 
     FROM TABLE(xmlsequence(xmlrow.extract('/row/Address/LocalAddress'))) 
    ) 
     LOOP 
     -- extract address values same as above. 
     SELECT extractValue(xmlrow,'/LocalAddress/City/text()') 
     INTO v_city 
     FROM dual; 
     DBMS_OUTPUT.put_line(v_city); 
     END LOOP; 
    END LOOP; 
    END; 

如何可以解析:在Oracle中整個XML?我發送這個xmltype變量。

這是給我一個comilation錯誤

Error report - 
ORA-06550: line 35, column 27: 
PLS-00382: expression is of wrong type 
ORA-06550: line 35, column 14: 
PL/SQL: ORA-00932: inconsistent datatypes: expected - got - 
ORA-06550: line 35, column 7: 
PL/SQL: SQL Statement ignored 
ORA-06550: line 41, column 37: 
PLS-00302: component 'EXTRACT' must be declared 
ORA-06550: line 41, column 37: 
PLS-00302: component 'EXTRACT' must be declared 
ORA-06550: line 41, column 30: 
PL/SQL: ORA-00904: "XMLROW"."EXTRACT": invalid identifier 
ORA-06550: line 40, column 7: 
PL/SQL: SQL Statement ignored 
ORA-06550: line 45, column 29: 
PLS-00382: expression is of wrong type 
ORA-06550: line 45, column 16: 
PL/SQL: ORA-00932: inconsistent datatypes: expected - got - 
ORA-06550: line 45, column 9: 
PL/SQL: SQL Statement ignored 
06550. 00000 - "line %s, column %s:\n%s" 
*Cause: Usually a PL/SQL compilation error. 

請幫我解決這個問題。 在此先感謝。

回答

2

我能夠使用這種方法

SET serveroutput ON format wrapped; 
DECLARE 
    x XMLType := XMLType(' 
<person> 
<row>  
<name>Tom</name>  
<Address>   
<LocalAddress>   
<State>California</State>   
<City>Los angeles</City>   
</LocalAddress>   
<LocalAddress>   
<State>California1</State>   
<City>Los angeles1</City>   
</LocalAddress>  
</Address> 
</row> 
<row>  
<name>Jim</name>  
<Address>  
<LocalAddress> 
<State>California</State>   
<City>Los angeles</City>  
</LocalAddress> 
</Address> 
</row> 
</person>'); 
BEGIN 
    FOR r IN 
    (SELECT ExtractValue(Value(p),'/row/name/text()') AS name , 
    Extract(Value(p),'/row/Address') As Address 
    FROM TABLE(XMLSequence(Extract(x,'/person/row'))) p 
) 
    LOOP 
    DBMS_OUTPUT.put_line(r.name); 
    FOR row1 IN 
    (SELECT ExtractValue(Value(l),'/LocalAddress/City/text()') AS city 
    FROM TABLE(XMLSequence(Extract(r.Address,'/Address/LocalAddress'))) l 
    ) 
    LOOP 

     DBMS_OUTPUT.put_line(row1.city); 
     -- do whatever you want with r.name, r.state, r.city 
    END LOOP; 
    -- do whatever you want with r.name, r.state, r.city 
    END LOOP; 
END; 
0

當您使用XML類型時,我假設您需要訪問單個標籤及其各自的值。下面是一段代碼示例,它將根據上述XML結構從每行中選擇名稱。注意:xml_in只是存儲過程中xmltype變量的變量名稱。

select extractValue(column_value,'/row/name/text()') as name 
from table (XMLSequence(xml_in.extract('/person/row'))); 

如果你需要做的嵌套解析,你也可以這樣做:

for xmlrow in (select column_value as xml from table(xmlsequence(xml_in.extract('/person/row'))))) loop 

    select extractValue(xmlrow.xml,'/row/name/text()') 
    into v_person_name 
    from dual; 

    for address in (select column_value as xml from table(xmlsequence(xmlrow.xml.extract('/row/Address/LocalAddress')))) loop 
     -- extract address values same as above. 
    end loop; 
end loop; 
+0

感謝解析嵌套的XML,但如何提取地址部分。 – user1685652

+0

只需使用XPath即可。所以extractValue(column_value,'/ row/Address/LocalAddress/State/text()')等等。 – Nick

+0

它是一個動態的XML,我不知道有多少本地地址我可以得到內部地址。 – user1685652