Thursday, April 27, 2017

Export / Download SAP ABAP Internal Table to XML

Hello, in this tutorial I will share on of way to export or download your internal table into XML file by the help of XML Transformations. There are 3 stages in the procedure which are:

  1. Create Table Type:
    1. Go to SE11.
    2. Create a structure of your internal table
    3. Create a table type based on the structure
    4.  Make Sure to save and activate them
  2. Create Transformation:
    1. Go to tcode STRANS or XSLT_TOOL
    2. Create an S Simple Transformation
    3. Click the sparkling magic wand button a.k.a Edit Simple Transformation Graphically (Ctrl+Shift+F11)
    4. Insert New Root in Data Roots
    5. Fill in any root name of the xml for Root-Name and <Table_Type> created in first stage for Type-Name
    6. Then drag the data root into Simple Transformation
    7. You might want to change the Field name in the XML by double click the element
    8. You can make the field into an attribute.
    9. Save Activate the transformation
  3. Complete your Program:
    1. Make sure you already have the particular internal table filled with data
    2. Prepare the output location
    3. Insert, modify and perform the following FORM.
FORM f_xml_export.
  DATA: lo_xml_doc TYPE ref TO CL_XML_DOCUMENT,
        lv_string TYPE string,
        lv_file TYPE string.

  CHECK <YOUR_INTERNAL_TABLE>[] IS NOT INITIAL.

*File path
  lv_file = '<YOUR_FILE_PATH>'.

*Transform internal table to XML DOM
  CALL TRANSFORMATION <YOUR_TRANSFORMATION_NAME>
  SOURCE <YOUR_ROOT_NAME> = <YOUR_INTERNAL_TABLE[]
  RESULT XML lv_string.

CREATE OBJECT lo_xml_doc.
lo_xml_doc->parse_string( lv_string ).
* Render To XML Table
data it_xml TYPE DCXMLLINES.
CALL FUNCTION 'SDIXML_DOM_TO_XML'
  EXPORTING
    document            = lo_xml_doc->m_document
   PRETTY_PRINT        = 'X'
* IMPORTING
*   XML_AS_STRING       =
*   SIZE                =
 TABLES
   XML_AS_TABLE        = it_xml
* EXCEPTIONS
*   NO_DOCUMENT         = 1
*   OTHERS              = 2
          .
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename     = lv_file
      filetype     = 'BIN'
    TABLES
      data_tab     = it_xml
    EXCEPTIONS
      OTHERS       = 1.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM.                    " F_XML_EXPORT

No comments:

Post a Comment