Parsing XML to internal table in SAP without transformation

To parse the XML data into SAP Internal Tables, generally we write Transformation using TCode XSLT_TOOL and need to call that using CALL TRANSFORMATION keyword.

There is another way to do the same using FM: SMUM_XML_PARSE

In this tutorial, we will pick the XML file stored on Application server and parse that file in ITAB. File: Test_Man.xml is stored on Application server path: Test_Man.xml

Application Server File:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GMInfo>
#<SenderID>987654321</SenderID>
#<ReceiverID>RCV1111111</ReceiverID>
#<MessageID>MID2222222</MessageID>
#<MessageDateTime>20210112000401</MessageDateTime>
#<LineItem>
##<SupplierCode>1</SupplierCode>
##<GINo>90000000000001</GINo>
##<DeliveryNo>8000000001</DeliveryNo>
##<SKUNo>MAT001</SKUNo>
#</LineItem>
#<LineItem>
##<SupplierCode>2</SupplierCode>
##<GINo>90000000000002</GINo>
##<DeliveryNo>8000000002</DeliveryNo>
##<SKUNo>MAT002</SKUNo>
#</LineItem>
#<LineItem>
##<SupplierCode>3</SupplierCode>
##<GINo>90000000000003</GINo>
##<DeliveryNo>8000000003</DeliveryNo>
##<SKUNo>MAT003</SKUNo>
#</LineItem>
</GMInfo>

A mapping structure needs to be created in SE11 according to XML

Line type for Table type ZTT_GRAYMARKET_ITM is ZGRAYMARKET_ITM

Source Code:
DATA : _file_string       TYPE xstring,
       _t_xml_data        TYPE TABLE OF smum_xmltb,
       _t_return          TYPE TABLE OF bapiret2,
       _t_zgraymarketinfo TYPE STANDARD TABLE OF zgraymarketinfo,
       _s_zgraymarketinfo TYPE zgraymarketinfo,
       _t_gminfo_itm      TYPE ztt_graymarket_itm,
       _s_gminfo_itm      TYPE zgraymarket_itm.

PARAMETERS : p_file TYPE rlgrap-filename.

START-OF-SELECTION.

* Open File
  OPEN DATASET p_file FOR INPUT IN BINARY MODE.
  IF sy-subrc IS INITIAL.
* Read file content
    READ DATASET p_file INTO _file_string.
    IF _file_string IS NOT INITIAL.
      DATA(_o_xmldoc) = NEW cl_xml_document( ).
      CALL METHOD _o_xmldoc->parse_xstring
        EXPORTING
          stream  = _file_string
        RECEIVING
          retcode = DATA(_retcode).
      IF _retcode IS INITIAL.
        REFRESH : _t_xml_data[], _t_return[].
* Read XML Data
        CALL FUNCTION 'SMUM_XML_PARSE'
          EXPORTING
            xml_input = _file_string
          TABLES
            xml_table = _t_xml_data
            return    = _t_return.
        IF line_exists( _t_return[ type = 'E' ] ).
          MESSAGE s208(00) WITH 'Error while parsing the file' DISPLAY LIKE 'E'.
          LEAVE LIST-PROCESSING.
        ENDIF.
        DELETE _t_xml_data WHERE hier EQ 1.
* Populate Internal Tables
        LOOP AT _t_xml_data ASSIGNING FIELD-SYMBOL(<fs_xml_data>).
          IF <fs_xml_data>-hier EQ 2 AND <fs_xml_data>-type EQ 'V'.
            ASSIGN COMPONENT <fs_xml_data>-cname OF STRUCTURE _s_zgraymarketinfo TO FIELD-SYMBOL(<fs_val>).
            IF sy-subrc IS INITIAL AND <fs_val> IS ASSIGNED.
              <fs_val> = <fs_xml_data>-cvalue.
            ENDIF.
          ELSEIF <fs_xml_data>-hier EQ 3 AND <fs_xml_data>-type EQ 'V'.
            UNASSIGN <fs_val>.
            ASSIGN COMPONENT <fs_xml_data>-cname OF STRUCTURE _s_gminfo_itm TO <fs_val>.
            IF sy-subrc IS INITIAL AND <fs_val> IS ASSIGNED.
              <fs_val> = <fs_xml_data>-cvalue.
            ENDIF.
          ELSEIF <fs_xml_data>-hier EQ 2 AND <fs_xml_data>-type IS INITIAL AND <fs_xml_data>-cname EQ 'LineItem'.
            IF _s_gminfo_itm IS NOT INITIAL.
              APPEND _s_gminfo_itm TO _t_gminfo_itm.
              CLEAR : _s_gminfo_itm.
            ELSE.
              CONTINUE.
            ENDIF.
          ENDIF.
        ENDLOOP.
* Append Last Row
        IF _s_gminfo_itm IS NOT INITIAL.
          APPEND _s_gminfo_itm TO _t_gminfo_itm.
          CLEAR : _s_gminfo_itm.
        ENDIF.
      ELSE.
        MESSAGE s208(00) WITH 'Wrong file format' DISPLAY LIKE 'E'.
        LEAVE LIST-PROCESSING.
      ENDIF.
    ENDIF.
    CLOSE DATASET p_file.
    IF _s_zgraymarketinfo IS NOT INITIAL.
      _s_zgraymarketinfo-lineitem = _t_gminfo_itm[].
      APPEND _s_zgraymarketinfo TO _t_zgraymarketinfo.
    ENDIF.
    REFRESH : _t_gminfo_itm[].
    CLEAR : _s_zgraymarketinfo, _s_gminfo_itm.
  ELSE.
    MESSAGE s208(00) WITH 'Error while accessing the file' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
* Print Output
  LOOP AT _t_zgraymarketinfo ASSIGNING FIELD-SYMBOL(<fs_zgraymarketinfo>).
    WRITE : / 'senderid:', <fs_zgraymarketinfo>-senderid,
            / 'receiverid:', <fs_zgraymarketinfo>-receiverid,
            / 'messageid:', <fs_zgraymarketinfo>-messageid,
            / 'shipto:', <fs_zgraymarketinfo>-shipto,
            / 'messagedatetime:', <fs_zgraymarketinfo>-messagedatetime.
    LOOP AT <fs_zgraymarketinfo>-lineitem ASSIGNING FIELD-SYMBOL(<fs_lineitem>).
      WRITE : / 'suppliercode:', <fs_lineitem>-suppliercode,
              / 'gino:', <fs_lineitem>-gino,
              / 'deliveryno:', <fs_lineitem>-deliveryno,
              / 'skuno:', <fs_lineitem>-skuno.
    ENDLOOP.
  ENDLOOP.

Output:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s