CREATE OR REPLACE PROCEDURE DOMSAMPLE(P_CLOB IN CLOB) IS
/**********************************************************************/
-- THIS PROCEDURE READS THE INPUT GIVEN IN THE CLOB VARIABLE AND GETS THE ELEMEN
TS
-- AND PRINTS IT OUT FROM THE XML BUILD AND ALSO CAN READ THE XML FILE WITH FEW
MODIFICATIONS
/*******************************************************************************
***************/
P xmlparser.parser;
doc xmldom.DOMDocument;
/***************************************************************/
-- prints elements in a document
procedure printElements(doc xmldom.DOMDocument) is
nl xmldom.DOMNodeList;
len number;
n xmldom.DOMNode;
eleval VARCHAR2(40);
V_CLOB CLOB;
begin
-- get all elements
nl := xmldom.getElementsByTagName(doc, '*');
len := xmldom.getLength(nl);
-- loop through elements
for i in 0..len-1 loop
n := xmldom.item(nl, i);
--get the text node associated with the element node
n:= xmldom.getfirstchild(n);
if xmldom.getnodetype(n) = 3 then
--dbms_output.put_line ('node 3 val>> '||xmldom.getnodevalue(n));
V_CLOB := V_CLOB||CHR(10)||XMLDOM.GETNODEVALUE(N);
end if;
end loop;
dbms_output.put_line(' CLOB IN SAMPLE2 ' ||V_CLOB);
end printElements;
/***************************************************************/
-- prints the attributes of each element in a document
procedure printElementAttributes(doc xmldom.DOMDocument) is
nl xmldom.DOMNodeList;
len1 number;
len2 number;
n xmldom.DOMNode;
e xmldom.DOMElement;
nnm xmldom.DOMNamedNodeMap;
attrname varchar2(100);
attrval varchar2(100);
begin
-- get all elements
nl := xmldom.getElementsByTagName(doc, '*');
len1 := xmldom.getLength(nl);
-- loop through elements
for j in 0..len1-1 loop
n := xmldom.item(nl, j);
e := xmldom.makeElement(n);
-- get all attributes of element
nnm := xmldom.getAttributes(n);
if (xmldom.isNull(nnm) = FALSE) then
len2 := xmldom.getLength(nnm);
-- loop through attributes
for i in 0..len2-1 loop
n := xmldom.item(nnm, i);
attrname := xmldom.getNodeName(n);
attrval := xmldom.getNodeValue(n);
-- dbms_output.put('getnode name ' || attrname || ' = ' || attrval);
end loop;
--dbms_output.put_line('');
end if;
end loop;
end printElementAttributes;
/***************************************************************/
begin
-- new parser
p := xmlparser.newParser;
-- set some characteristics
xmlparser.setValidationMode(p, FALSE);
--#PARSE CLOB
XMLPARSER.PARSECLOB(P,P_CLOB);
-- get document
doc := xmlparser.getDocument(p);
-- Print document elements
printElements(doc);
-- Print document element attributes
-- printElementAttributes(doc);
-- deal with exceptions
exception
when xmldom.INDEX_SIZE_ERR then
--dbms_output.put_line(-20120, 'Index Size error');
RAISE_APPLICATION_ERROR(-20120, 'Index Size error');
when xmldom.DOMSTRING_SIZE_ERR then
RAISE_APPLICATION_ERROR(-20120, 'String Size error');
when xmldom.HIERARCHY_REQUEST_ERR then
RAISE_APPLICATION_ERROR(-20120, 'Hierarchy request error');
when xmldom.WRONG_DOCUMENT_ERR then
RAISE_APPLICATION_ERROR(-20120, 'Wrong doc error');
when xmldom.INVALID_CHARACTER_ERR then
RAISE_APPLICATION_ERROR(-20120, 'Invalid Char error');
when xmldom.NO_DATA_ALLOWED_ERR then
RAISE_APPLICATION_ERROR(-20120, 'Nod data allowed error');
when xmldom.NO_MODIFICATION_ALLOWED_ERR then
RAISE_APPLICATION_ERROR(-20120, 'No mod allowed error');
when xmldom.NOT_FOUND_ERR then
RAISE_APPLICATION_ERROR(-20120, 'Not found error');
when xmldom.NOT_SUPPORTED_ERR then
RAISE_APPLICATION_ERROR(-20120, 'Not supported error');
when xmldom.INUSE_ATTRIBUTE_ERR then
RAISE_APPLICATION_ERROR(-20120, 'In use attr error');
end domsample;