ORACLE 解析xml字串-轉載的

來源:互聯網
上載者:User

標籤:

1.xml字串

/*

<orderlist>
        <order>
        <orderid>1</orderid>
        <ordernumber>857544544</ordernumber>
        <orderprice>54</orderprice>
        </order>
        
        <order>
        <orderid>2</orderid>
        <ordernumber>858544544</ordernumber>
        <orderprice>63</orderprice>
        </order>
        
        <order>
        <orderid>3</orderid>
        <ordernumber>454854555</ordernumber>
        <orderprice>781</orderprice>
        </order>        
</orderlist>

 

*/

 

2.oracle預存程序

 

CREATE OR REPLACE PROCEDURE p_xmlparse (p_xml   IN     CLOB,
                                                                     r_cur      OUT SYS_REFCURSOR)  
        AS  
           /***  
           xml格式<?xml version="1.0" encoding="utf-8"?>  
           <material id="101">  
               <attribute0>value0</attribute0>  
               <attribute1>value1</attribute1>  
               <attribute2>value2</attribute2>  
           </material>  
           ***/  
        -- xml解析器  
           xmlpar          xmlparser.parser := xmlparser.newparser;  
        -- dom文檔對象  
           doc             xmldom.domdocument;  
           materialnodes   xmldom.domnodelist;  
           materialid      VARCHAR2 (50);  
           chilnodes       xmldom.domnodelist;  
           tempnode        xmldom.domnode;  
           temparrmap      xmldom.domnamednodemap;  
        -- 以下變數用於擷取xml節點的值  
           v_attribute     VARCHAR2 (50);  
           v_value         VARCHAR2 (50);  
           tmp             INTEGER;  
           l_sql           VARCHAR2 (32767) := ‘select ‘;  
        BEGIN  
           xmlparser.parseclob (xmlpar, p_xml);  
           doc := xmlparser.getdocument (xmlpar);  
        -- 釋放解析器執行個體  
           xmlparser.freeparser (xmlpar);  
           materialnodes := xmldom.getelementsbytagname (doc, ‘material‘);  
           tempnode := xmldom.item (materialnodes, 0);  
        -- 擷取根項目的ID屬性值  
           materialid := xmldom.getattribute (xmldom.getdocumentelement (doc), ‘id‘);  
        -- 所有屬性  
           temparrmap := xmldom.getattributes (tempnode);  
        -- 擷取子項目的值  
           chilnodes := xmldom.getchildnodes (tempnode);  
           tmp := xmldom.getlength (chilnodes);  
           l_sql := l_sql || materialid || ‘ as materialid‘;  
        FOR i IN 0 .. tmp - 1  
           LOOP  
              v_attribute := xmldom.getnodename (xmldom.item (chilnodes, i));  
              v_value :=  
                 xmldom.getnodevalue (  
                    xmldom.getfirstchild (xmldom.item (chilnodes, i)));  
              l_sql := l_sql || ‘,‘‘‘ || v_value || ‘‘‘ as ‘ || v_attribute;  
        -- dbms_output.put_line(materialid||‘ ‘||v_attribute||‘ ‘||v_value);  
        END LOOP;  
           l_sql := l_sql || ‘ from dual‘;  
        -- DBMS_OUTPUT.put_line (l_sql);  
        OPEN r_cur FOR l_sql;  
        -- 釋放文檔對象  
           xmldom.freedocument (doc);  
        EXCEPTION  
        WHEN OTHERS  
        THEN  
              DBMS_OUTPUT.put_line (SQLERRM);  
        END p_xmlparse; 

 

 

3.執行結果

來源文件地址:http://blog.csdn.net/wzy0623/article/details/8245062

ORACLE 解析xml字串-轉載的

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.