0% found this document useful (0 votes)
102 views

Parsing XML

This procedure parses an XML document passed into it as a CLOB. It extracts the elements and their text node values, appending them to another CLOB variable. It also has the ability to extract and output the attributes of each element. The procedure handles exceptions that may occur during the parsing and DOM manipulation.

Uploaded by

sagarka21
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Parsing XML

This procedure parses an XML document passed into it as a CLOB. It extracts the elements and their text node values, appending them to another CLOB variable. It also has the ability to extract and output the attributes of each element. The procedure handles exceptions that may occur during the parsing and DOM manipulation.

Uploaded by

sagarka21
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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;

You might also like