ORACLE parsing XML string-reproduced

Source: Internet
Author: User
Tags xml parser

1.xml string

/*

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

CREATE OR REPLACE PROCEDURE p_xmlparse (p_xml in CLOB,
R_cur out Sys_refcursor)
As
/***
XML format <?xml version= "1.0" encoding= "Utf-8"?>
<material id= "101" >
<attribute0>value0</attribute0>
<attribute1>value1</attribute1>
<attribute2>value2</attribute2>
</material>
***/
--XML Parser
Xmlpar Xmlparser.parser: = Xmlparser.newparser;
--DOM Document object
Doc Xmldom.domdocument;
Materialnodes xmldom.domnodelist;
Materialid VARCHAR2 (50);
Chilnodes xmldom.domnodelist;
Tempnode Xmldom.domnode;
Temparrmap Xmldom.domnamednodemap;
--The following variables are used to get the value of the XML node
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);
--Releasing the parser instance
Xmlparser.freeparser (Xmlpar);
Materialnodes: = Xmldom.getelementsbytagname (doc, ' material ');
Tempnode: = Xmldom.item (materialnodes, 0);
--Gets the id attribute value of the root element
Materialid: = Xmldom.getattribute (Xmldom.getdocumentelement (DOC), ' id ');
--All properties
Temparrmap: = Xmldom.getattributes (Tempnode);
--Get the value of the child element
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;
--Releasing the Document object
Xmldom.freedocument (DOC);
EXCEPTION
When OTHERS
Then
Dbms_output.put_line (SQLERRM);
END P_xmlparse;

3. Implementation results

Source Document Address: http://blog.csdn.net/wzy0623/article/details/8245062

ORACLE parsing XML string-reproduced

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.