Referring to: the Oracle stored procedure using a cursor to the field inside the bulk parsed xml string CLOB
Background : In writing stored procedures, you need to get information form submission. Information is in the form colb type of field dataxml, how to get the string stored in xml format it? Referring Baidu content, a write function (function), xml format string parameters (dataxml value) and a specified node (the nodeName), returns the value of the specified node (nodeValue).
Coding :
--get_xml_nodeValue achieve start
CREATE OR REPLACE FUNCTION get_xml_nodeValue(xmlStr CLOB, nodeName VARCHAR2) RETURN VARCHAR2 IS
- Create xml parser instance xmlparser.Parser
xmlPar xmlparser.Parser: = xmlparser.newParser;
- the definition of a DOM document
xDoc xmldom.DOMDocument;
- the definition of item the child node number of variables
lenItme INTEGER;
- defined list of nodes, the nodes store item they
itemNodes xmldom.DOMNodeList;
- defined nodes, nodes store a single item
itemNode xmldom.DOMNode;
valueReturn VARCHAR2 (100);
The BEGIN
- Analytical xmlStr in xml string, and place the xmlPar in
xmlparser.parseClob (xmlPar, xmlStr);
- the dump data xmlPar dom document
xDoc: = xmlparser.getDocument (xmlPar);
- release parser instance
xmlparser.freeParser (xmlPar);
- an overview of all item node
itemNodes: = xmldom.getElementsByTagName (xDoc, the nodeName);
- the number of nodes acquired item
lenItme: = xmldom.getLength (itemNodes);
- if no the label, return EMPTY
the iF lenItme = 0 tHEN
valueReturn: = '';
the ELSE
- obtaining a first node in a list item node
itemNode: = xmldom.item (itemNodes, 0);
- Gets the value of all the child nodes
valueReturn: = xmldom.getNodeValue (xmldom.getFirstChild (itemNode));
the END the IF;
- release dom
xmldom.freeDocument(xDoc);
RETURN valueReturn;
END get_xml_nodeValue;
--get_xml_nodeValue achieve end
Test call :
表:FORM_DATA;
字段:DATAXML,字段值:“<data><id>1</id><name>张三</name><phone>17700000000</phone><id_number>410527999909099999</id_number></data>”;
Query 1: select get_xml_nodeValue (f.dataxml, 'name') from form_data f; Results: Zhang;
Query 2: select get_xml_nodeValue (f.dataxml, 'phone') from form_data f; Results: 17700000000;
Query 3: select get_xml_nodeValue (f.dataxml, 'id_number') from form_data f; Results: 410527999909099999.
Finish!