Tuesday, 24 October 2017

Parsing an XML in PL/SQL using DOM Parser API

Following Example demonestrates the Parsing an XML Document using PL/SQL DOM API.
  1. CREATE OR REPLACE PROCEDURE PARSE_GENERIC_XML_DOM  
  2. is  
  3.   l_xml_data        CLOB;  
  4.   l_parser          DBMS_XMLParser.Parser;  
  5.   l_document        DBMS_XMLDom.DomDocument;  
  6.   l_element         DBMS_XMLDom.DomElement;  
  7.   l_node_list       DBMS_XMLDom.DomNodeList;  
  8.   l_node            DBMS_XMLDom.DomNode;  
  9.   l_attr_node_map   Dbms_Xmldom.Domnamednodemap;  
  10.   l_attr_node       Dbms_Xmldom.Domnode;     
  11.   l_node_length number;  
  12.   l_attribute_length number;  
  13.   l_attribute_name          varchar2(100);  
  14.   l_attribute_value         varchar2(100);      
  15.   l_node_name               varchar2(50);  
  16.   l_node_value              varchar2(50);    
  17.   l_namespace               varchar2(50);    
  18. BEGIN  
  19.   l_xml_data := '<a>Test<Hi>TestVal</Hi><department deptno="10" dname="ACCOUNTING" location="NEW YORK">  
  20.                    <employee empno="7782" ename="CLARK">  
  21.                       <job Id="20">MANAGER</job>  
  22.                       <mrg>7839</mrg>  
  23.                       <hiredate>6/9/1981</hiredate>  
  24.                       <sal>2450.00</sal>  
  25.                    </employee>  
  26.                    <employee empno="7839" ename="KING">  
  27.                       <job>PRESIDENT</job>  
  28.                       <mrg></mrg>  
  29.                       <hiredate>11/17/1981</hiredate>  
  30.                       <sal>5000.00</sal>  
  31.                    </employee>  
  32.                    <employee empno="7934" ename="MILLER">  
  33.                       <job>CLERK</job>  
  34.                       <mrg>7782</mrg>  
  35.                       <hiredate>1/23/1982</hiredate>  
  36.                       <sal>1300.00</sal>  
  37.                    </employee>  
  38.                 </department></a>';  
  39.     dbms_output.put_line(l_xml_data);  
  40.     l_parser := dbms_xmlparser.newParser;  
  41.     dbms_xmlparser.parseClob(l_parser,l_xml_data);  
  42.     l_document:= dbms_xmlparser.getDocument(l_parser);  
  43.     dbms_xmlparser.freeParser(l_parser);      
  44.     l_element := dbms_xmldom.getDocumentElement(l_document);  
  45.     l_namespace  := xmldom.getnamespace(l_element);  
  46.     dbms_output.put_line('XML Root element information');  
  47.     dbms_output.put_line('Qualified Name: ' || dbms_xmldom.getqualifiedname(l_element));  
  48.     dbms_output.put_line('Local Name: ' || dbms_xmldom.getlocalname(l_element));  
  49.     dbms_output.put_line('Namespace: ' || l_namespace);  
  50.     dbms_output.put_line('Expanded Name: ' || dbms_xmldom.getexpandedname(l_element));                
  51.     l_node_list := xmldom.getElementsByTagName(l_document, '*');  
  52.     l_node_length := xmldom.getLength(l_node_list);  
  53.     dbms_output.put_line('Node Length:'||l_node_length);  
  54.     -- Loop through elements  
  55.     FOR j in 0..l_node_length-1   
  56.     LOOP  
  57.       l_node := xmldom.item(l_node_list, j);       
  58.       l_node_name := dbms_xmldom.getNodeName(l_node);        
  59.       l_node_value :=  Dbms_Xmldom.Getnodevalue(dbms_xmldom.getFirstChild(l_node));  
  60.       dbms_output.put_line('Node Name :'||l_node_name||' Node Value :'||l_node_value);  
  61.       l_attr_node_map := xmldom.getAttributes(l_node);  
  62.   
  63.      IF (xmldom.isNull(l_attr_node_map) = FALSE)   
  64.      THEN  
  65.         l_attribute_length := xmldom.getLength(l_attr_node_map);  
  66.         dbms_output.put_line('Attribute Length:'||l_attribute_length);  
  67.         --Loop through attributes  
  68.         FOR i in 0..l_attribute_length-1   
  69.         LOOP  
  70.            l_attr_node := xmldom.item(l_attr_node_map, i);  
  71.            l_attribute_name := xmldom.getNodeName(l_attr_node);  
  72.            l_attribute_value := xmldom.getNodeValue(l_attr_node);  
  73.            dbms_output.put_line('Attribute Name: ' || l_attribute_name || ' = ' || l_attribute_value);  
  74.         END LOOP;          
  75.       END IF;  
  76.     END LOOP;    
  77.     dbms_xmldom.freeDocument(l_document);  
  78. EXCEPTION  
  79.   WHEN OTHERS   
  80.   THEN  
  81.     DBMS_OUTPUT.PUT_LINE(SQLCODE||':'||SQLERRM);  
  82. END;  
  83. /  




















1 comment:

  1. 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