GRANT javauserpriv to scott;
GRANT javasyspriv to scott;
GRANT EXECUTE ON xmldom TO scott;
CREATE SYNONYM scott.xmldom FOR SYS.xmldom;
CREATE TABLE xmldom_test (xml_dom_page CLOB);
*/
DECLARE
doc xmldom.DOMDocument;
main_node xmldom.DOMNode;
root_node xmldom.DOMNode;
user_node xmldom.DOMNode;
item_node xmldom.DOMNode;
root_elmt xmldom.DOMElement;
item_elmt xmldom.DOMElement;
item_text xmldom.DOMText;
lv_xml_type XMLTYPE;
lv_xml_data CLOB;
CURSOR get_users(p_deptno NUMBER) IS
SELECT empno
, ename
, deptno
, sal
, comm
, rownum
FROM emp
WHERE deptno = p_deptno;
BEGIN
doc := xmldom.newDOMDocument;
main_node := xmldom.makeNode(doc);
root_elmt := xmldom.createElement(
doc
, 'EMPSET'
);
root_node := xmldom.appendChild(
main_node
, xmldom.makeNode(root_elmt)
);
FOR get_users_rec IN get_users(10) LOOP
item_elmt := xmldom.createElement(
doc
, 'EMP'
);
xmldom.setAttribute(
item_elmt
, 'num'
, get_users_rec.rownum
);
user_node := xmldom.appendChild(
root_node
, xmldom.makeNode(item_elmt)
);
--//--------------Empno-----------
item_elmt := xmldom.createElement(
doc
, 'EMP_NO'
);
item_node := xmldom.appendChild(
user_node
, xmldom.makeNode(item_elmt)
);
item_text := xmldom.createTextNode(
doc
, get_users_rec.empno
);
item_node := xmldom.appendChild(
item_node
, xmldom.makeNode(item_text)
);
--//--------------Ename-----------
item_elmt := xmldom.createElement(
doc
, 'EMP_NAME'
);
item_node := xmldom.appendChild(
user_node
, xmldom.makeNode(item_elmt)
);
item_text := xmldom.createTextNode(
doc
, get_users_rec.ename
);
item_node := xmldom.appendChild(
item_node
, xmldom.makeNode(item_text)
);
--//--------------Salary-----------
item_elmt := xmldom.createElement(
doc
, 'Salary'
);
item_node := xmldom.appendChild(
user_node
, xmldom.makeNode(item_elmt)
);
item_text := xmldom.createTextNode(
doc
, get_users_rec.sal
);
item_node := xmldom.appendChild(
item_node
, xmldom.makeNode(item_text)
);
--//--------------Comm-----------
item_elmt := xmldom.createElement(
doc
, 'Commission'
);
item_node := xmldom.appendChild(
user_node
, xmldom.makeNode(item_elmt)
);
item_text := xmldom.createTextNode(
doc
, get_users_rec.comm
);
item_node := xmldom.appendChild(
item_node
, xmldom.makeNode(item_text)
);
END LOOP;
--xmldom.writeToFile(doc , '/u01/oracle/PRJ7D/db/tech_st/12.1.0/rdbms/log/docSample.xml');
lv_xml_type := xmldom.getxmltype (doc);
lv_xml_data := lv_xml_type.getClobVal ();
insert into xmldom_test values ( lv_xml_data);
xmldom.freeDocument(doc);
END;
OUTPUT:
<EMPSET>
<EMP num="1">
<EMP_NO>7839</EMP_NO>
<EMP_NAME>KING</EMP_NAME>
<Salary>5000</Salary>
<Commission></Commission>
</EMP>
<EMP num="2">
<EMP_NO>7782</EMP_NO>
<EMP_NAME>CLARK</EMP_NAME>
<Salary>2450</Salary>
<Commission></Commission>
</EMP>
<EMP num="3">
<EMP_NO>7934</EMP_NO>
<EMP_NAME>MILLER</EMP_NAME>
<Salary>1300</Salary>
<Commission></Commission>
</EMP>
</EMPSET>
No comments:
Post a Comment