Oracle uses PLSQL to process XML

1 Overview

In oracle8i and later versions, the xmldom method is provided to realize the processing of XML format data, but in the actual business information system application development, more are used in the program (such as java, .net and other development languages The provided xml component) to parse XML data, rarely directly parse XML data directly in the ORACLE database.

2. Processing process

XML data can be a character string (suitable for processing smaller xml data), physical files or CLOB fields (for processing larger xml data fields); the following XML data is used as an example to describe the parsing process.

An example of the XML data format is as follows:

<xml version="1.0" encoding="GBK">
    <items>
        <unit>
            <item>
                <name>单位名称</name>
                <value>JXDL</value>
            </item>
            <item>
                <name>年新增归档数</name>
                <value>330</value>
            </item>
        </unit>
    </items>
</xml>

(1) Define the XML rules for parsing

Define or determine the XML rules to parse, as above.

(2) Create an XML parser

Create an XML parser instance XMLPARSER.parser

xmlPar XMLPARSER.parser := XMLPARSER.NEWPARSER;

(3) Define the DOM document object

doc xmldom.DOMDocument;

(4) Define other objects

Define other objects needed to parse XML, as follows:

lenUnit integer;
lenItem integer;
unitNodes xmldom.DOMNodeList;
itemNodes xmldom.DOMNodeList;
chilNodes xmldom.DOMNodeList;
tempNode_unit xmldom.DOMNode;
tempNode xmldom.DOMNode;
tempArrMap xmldom.DOMNamedNodeMap;
--================================
--以下变量用于获取XML节点的值
name varchar2(50);
value varchar2(20);
tmp integer;
--================================
xmlClobData clob;

(5) Get xml data

To obtain xml data, the following assumptions are obtained from the clob field of the data table, as follows:

 select datastring into xmlClobData from p_xml_datainfo t where …… ;

(6) Parsing xml data

xmlPar := xmlparser.newParser; 
--xmlparser.parseBuffer(xmlPar,xmlString);--
xmlparser.parseClob(xmlPar,xmlClobData);
doc := xmlparser.getDocument( xmlPar );
-- 释放解析器实例
xmlparser.freeParser(xmlPar);
-- 获取所有unit元素
unitNodes := xmldom.getElementsByTagName( doc, 'unit' );
lenUnit := xmldom.getLength( unitNodes );
--遍历所有unit元素
FOR i in 0..lenUnit-1
LOOP
    --获取第i个unit
    tempNode_unit := xmldom.item( unitNodes, i );  
    itemNodes:=xmldom.getChildNodes(tempNode_unit); 
    lenItem := xmldom.getLength( itemNodes );
    FOR j in 0..lenItem-1
    LOOP
        tempNode := xmldom.item( itemNodes, j );     
        --获取子元素的值
        chilNodes := xmldom.getChildNodes(tempNode);
        tmp := xmldom.GETLENGTH( chilNodes );
        name := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 0 )));
        value := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 1 )));
        DBMS_output.PUT_LINE(i||j,name,value);
    end loop;
END LOOP;

(7) Release the document object

xmldom.freeDocument(doc);

(8) Exception and error handling

EXCEPTION
	WHEN OTHERS THEN
		DBMS_output.PUT_LINE(SQLERRM);

Guess you like

Origin blog.csdn.net/qq_25775675/article/details/131172705