Following Example demonestrates the Parsing an XML Document using PL/SQL DOM API.
- CREATE OR REPLACE PROCEDURE PARSE_GENERIC_XML_DOM
- is
- l_xml_data CLOB;
- l_parser DBMS_XMLParser.Parser;
- l_document DBMS_XMLDom.DomDocument;
- l_element DBMS_XMLDom.DomElement;
- l_node_list DBMS_XMLDom.DomNodeList;
- l_node DBMS_XMLDom.DomNode;
- l_attr_node_map Dbms_Xmldom.Domnamednodemap;
- l_attr_node Dbms_Xmldom.Domnode;
- l_node_length number;
- l_attribute_length number;
- l_attribute_name varchar2(100);
- l_attribute_value varchar2(100);
- l_node_name varchar2(50);
- l_node_value varchar2(50);
- l_namespace varchar2(50);
- BEGIN
- l_xml_data := '<a>Test<Hi>TestVal</Hi><department deptno="10" dname="ACCOUNTING" location="NEW YORK">
- <employee empno="7782" ename="CLARK">
- <job Id="20">MANAGER</job>
- <mrg>7839</mrg>
- <hiredate>6/9/1981</hiredate>
- <sal>2450.00</sal>
- </employee>
- <employee empno="7839" ename="KING">
- <job>PRESIDENT</job>
- <mrg></mrg>
- <hiredate>11/17/1981</hiredate>
- <sal>5000.00</sal>
- </employee>
- <employee empno="7934" ename="MILLER">
- <job>CLERK</job>
- <mrg>7782</mrg>
- <hiredate>1/23/1982</hiredate>
- <sal>1300.00</sal>
- </employee>
- </department></a>';
- dbms_output.put_line(l_xml_data);
- l_parser := dbms_xmlparser.newParser;
- dbms_xmlparser.parseClob(l_parser,l_xml_data);
- l_document:= dbms_xmlparser.getDocument(l_parser);
- dbms_xmlparser.freeParser(l_parser);
- l_element := dbms_xmldom.getDocumentElement(l_document);
- l_namespace := xmldom.getnamespace(l_element);
- dbms_output.put_line('XML Root element information');
- dbms_output.put_line('Qualified Name: ' || dbms_xmldom.getqualifiedname(l_element));
- dbms_output.put_line('Local Name: ' || dbms_xmldom.getlocalname(l_element));
- dbms_output.put_line('Namespace: ' || l_namespace);
- dbms_output.put_line('Expanded Name: ' || dbms_xmldom.getexpandedname(l_element));
- l_node_list := xmldom.getElementsByTagName(l_document, '*');
- l_node_length := xmldom.getLength(l_node_list);
- dbms_output.put_line('Node Length:'||l_node_length);
- -- Loop through elements
- FOR j in 0..l_node_length-1
- LOOP
- l_node := xmldom.item(l_node_list, j);
- l_node_name := dbms_xmldom.getNodeName(l_node);
- l_node_value := Dbms_Xmldom.Getnodevalue(dbms_xmldom.getFirstChild(l_node));
- dbms_output.put_line('Node Name :'||l_node_name||' Node Value :'||l_node_value);
- l_attr_node_map := xmldom.getAttributes(l_node);
- IF (xmldom.isNull(l_attr_node_map) = FALSE)
- THEN
- l_attribute_length := xmldom.getLength(l_attr_node_map);
- dbms_output.put_line('Attribute Length:'||l_attribute_length);
- --Loop through attributes
- FOR i in 0..l_attribute_length-1
- LOOP
- l_attr_node := xmldom.item(l_attr_node_map, i);
- l_attribute_name := xmldom.getNodeName(l_attr_node);
- l_attribute_value := xmldom.getNodeValue(l_attr_node);
- dbms_output.put_line('Attribute Name: ' || l_attribute_name || ' = ' || l_attribute_value);
- END LOOP;
- END IF;
- END LOOP;
- dbms_xmldom.freeDocument(l_document);
- EXCEPTION
- WHEN OTHERS
- THEN
- DBMS_OUTPUT.PUT_LINE(SQLCODE||':'||SQLERRM);
- END;
- /
how to handle multiple xml data, say for eg data is from cursor containing multiple xmls as of date so how to iterate the loop
ReplyDelete