Tuesday, November 19, 2019

XML Parsing Example

SET DEFINE OFF;
SET SERVEROUT ON;

DECLARE
   v_mds_xml_parser        DBMS_XMLPARSER.parser;
   v_mds_xml_doc           DBMS_XMLDOM.domdocument;
   v_inq_entrp_ordr_node   xmldom.domnode;
   v_OrderList             xmldom.domnodelist;
   v_orderlist_node        xmldom.domnode;
   v_activityType_list     xmldom.domnodelist;
   v_actvity_node          xmldom.domnode;
   text_node               xmldom.domnode;
   v_activityType          VARCHAR2 (200) := NULL;
   i_reqxml                CLOB
      := '<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 rel. 2 sp2 (x64) (http://www.altova.com)-->
<InquirePendingEnterpriseOrderResponse xsi:schemaLocation="http://csi.cingular.com/CSI/Namespaces/Container/Public/InquirePendingEnterpriseOrderResponse.xsd InquirePendingEnterpriseOrderResponse.xsd" xmlns="http://csi.cingular.com/CSI/Namespaces/Container/Public/InquirePendingEnterpriseOrderResponse.xsd" xmlns:cdm="http://csi.att.com/CSI/Namespaces/GlobalIntegratedOrderManager/Types/Public/CommonDataModel.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <OrderList>
        <orderNumber>GIOM234</orderNumber>
        <orderType>MACDs</orderType>
        <activityType>GIOM234_1</activityType>
        <activityType>GIOM234_2</activityType>
    </OrderList>
    <OrderList>
        <orderNumber>GIOM789</orderNumber>
        <orderType>New Start</orderType>
        <activityType>GIOM789_1</activityType>
        <activityType>GIOM789_2</activityType>
    </OrderList>
    <Response>
        <cdm:code>String</cdm:code>
        <cdm:description>String</cdm:description>
    </Response>
</InquirePendingEnterpriseOrderResponse>
';

   V_actType_list          VARCHAR2 (1000) := NULL;
BEGIN
   v_mds_xml_parser := DBMS_XMLPARSER.newparser;
   xmlparser.parseclob (v_mds_xml_parser, i_reqxml);
   v_mds_xml_doc := DBMS_XMLPARSER.getdocument (v_mds_xml_parser);
   /* Application Details */
   v_inq_entrp_ordr_node :=
      xmldom.item (
         xmldom.getelementsbytagname (
            v_mds_xml_doc,
            'InquirePendingEnterpriseOrderResponse'),
         0);
   /*AccountOrganizationIdentifierContent */
   v_OrderList :=
      xmldom.getchildrenbytagname (
         xmldom.makeelement (v_inq_entrp_ordr_node),
         'OrderList');

   FOR l_index IN 0 .. (xmldom.getlength (v_OrderList)) - 1
   LOOP
      v_orderlist_node := xmldom.item (v_OrderList, l_index);
      v_activityType_list :=
         xmldom.getchildrenbytagname (xmldom.makeelement (v_orderlist_node),
                                      'activityType');
      DBMS_OUTPUT.put_line ('OrderList=' || l_index);

      FOR l_index IN 0 .. (xmldom.getlength (v_activityType_list)) - 1
      LOOP
         v_actvity_node := xmldom.item (v_activityType_list, l_index);
         text_node := DBMS_XMLDOM.getfirstchild (v_actvity_node);
         v_activityType := DBMS_XMLDOM.getnodevalue (text_node);
         V_actType_list := V_actType_list || '''' || v_activityType || ''',';
         DBMS_OUTPUT.put_line ('v_activityType=' || v_activityType);
      END LOOP;
   END LOOP;
END;
/

OrderList=0
v_activityType=GIOM234_1
v_activityType=GIOM234_2
OrderList=1
v_activityType=GIOM789_1
v_activityType=GIOM789_2

PL/SQL procedure successfully completed.

SQL>

No comments:

Post a Comment