How to Download an iDoc to XML File (3 Different Techniques)

If you want to download iDocs as files, there are several standard SAP options. But, if you want to convert an iDoc in your SAP system as an XML, capabilities and limited. You may need to download an iDoc created in the system in XML format for multiple reasons. You may want to communicate iDoc samples in XML format with third party systems for requirement analysis purposes. Especially in integration scenarios with external third party systems before setting up the actual interface, you may be required to share sample iDocs in XML formats with the third party system owners. The ABAP program I have created to convert a sample iDoc to a local XML file will be helpful in such scenarios.

In this article we will discuss:

  1. Custom ABAP Z Program Logic to Download or Convert iDocs to XML files.

  2. Alternative Methods to Download iDocs as XMLs.

  3. How to download iDoc data to other different file formats such as HTML, Spreadsheet, text etc.

SAP Versions used in the illustration:

  • SAP S4 HANA Fashion 1709

1. Custom Z ABAP Program to Download/Convert iDocs to XML files.

Selection screen:

Basic selection screen with two parameters to enter the iDoc number and the local directory path XML file would be saved. File name will be defaulted to idoc_<idoc number>.xml. More on save file dialog can be found in my previous blog post.

selection screen of ABAP program to download iDoc to XML file.
selection screen of ABAP program to download iDoc to XML file.



iDoc xml will be downloaded to given location in file path.

Download iDoc XML to local drive using ABAP program
Download iDoc XML to local drive


iDoc data downloaded in XML format to local drive using Z custom ABAP code
iDoc data downloaded in XML format


ABAP Code to download iDoc as XML:

The program was developed in SAP S4 HANA client, and it is compatible with newer SAP HANA versions. Copy the program code to a new executable Z ABAP program in transaction se38 to start using it.

*& Report Z_IDOC_TO_XML
REPORT z_idoc_to_xml.

*Global tables
DATA: gt_dyfields LIKE dynpread OCCURS 0 WITH HEADER LINE,   "screen fields
      gt_data_tab TYPE TABLE OF string WITH HEADER LINE.     "download data tab

*Global variables
DATA: gv_filename TYPE string VALUE 'idoc',                  "file name
      gv_path     TYPE string,                               "file path
      gv_result   TYPE i,
      gv_xml_str TYPE string.                                "XML string

DATA: idoc TYPE REF TO cl_idoc_xml1.                         "iDoc class instance

*Selection Screen
PARAMETERS: p_docnum LIKE edidc-docnum OBLIGATORY.            "iDoc number
PARAMETERS: p_path TYPE string LOWER CASE OBLIGATORY.         "File path

*Open file dialog: File location

  CLEAR gt_dyfields[].

* Append idoc document number field
  CLEAR gt_dyfields.
  gt_dyfields-fieldname = 'P_DOCNUM'.
  APPEND gt_dyfields TO gt_dyfields[].

* Read screen fields and values
      dyname               = sy-cprog
      dynumb               = sy-dynnr
      translate_to_upper   = 'X'
      dynpfields           = gt_dyfields
      invalid_abapworkarea = 1
      invalid_dynprofield  = 2
      invalid_dynproname   = 3
      invalid_dynpronummer = 4
      invalid_request      = 5
      no_fielddescription  = 6
      invalid_parameter    = 7
      undefind_error       = 8
      double_conversion    = 9
      stepl_not_found      = 10
      OTHERS               = 11.

* Read iDoc document number
  READ TABLE gt_dyfields WITH KEY fieldname = 'P_DOCNUM'.

  IF sy-subrc = 0.
    IF gt_dyfields-fieldvalue <> ''.
      SHIFT gt_dyfields-fieldvalue LEFT DELETING LEADING space.
      CONCATENATE gv_filename '_' gt_dyfields-fieldvalue INTO gv_filename.

* Display save dialog window
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
      default_extension = 'xml'
      default_file_name = gv_filename
      initial_directory = 'C:'
      filename          = gv_filename
      path              = gv_path
      fullpath          = p_path
      user_action       = gv_result.


* Create Object and create IDOC_XML
      docnum             = p_docnum
      error_loading_idoc = 1
      error_building_xml = 2
      OTHERS             = 3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
               RAISING no_idoc_xml_loaded.

* Transformation
  CALL METHOD idoc->get_xmldata_as_string
      data_string = gv_xml_str.

*append xml string to data tab
  APPEND gv_xml_str TO gt_data_tab.

*Download file
      filename                = p_path
      data_tab                = gt_data_tab
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      OTHERS                  = 22.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

2. Alternative method to download iDocs as XML.

If you do not want to create a new Z program in your system or you are not an ABAPer, you can configure a test partner profile using transaction we20 and configure an outbound profile with the desired message type/iDoc basic type. Assign a file port to the outbound partner profile configuration and reprocess the iDoc using transaction we19 or any other test tool. iDoc XML will be generated at SAP server location (al11) configured in file port.

iDoc XML downloaded to al11 location can be extracted to a local machine using transaction cg3z.

3. Download iDoc as Spreadsheet, HTML, text, etc.

If you want to download iDocs to other file formats such as Excel or html, there are several standard SAP options available.

  • Download iDoc using Transaction IDOC.

Using transaction iDoc and using option ‘Analyze iDoc Field values’ you can download iDoc data to multiple different formats.

Analyze iDoc field values in IDOC transaction
Analyze iDoc field values


But I was not able to convert the iDoc data to the external XML format.

download iDoc to different file formats
download iDoc to different file formats


  • Save iDoc as File using Transaction we02.

Using we02 you can also download iDoc to a local file.

Use we02 to download iDocs to excel, text, html and other formats
Use we02 to download iDocs


  • View XML format using Function Module IDOC_XML_TRANSFORM.

Go to transaction se37 and execute the FM IDOC_XML_TRANSFORM with iDoc number as input. You will be able to iDoc control record and data segments.

View iDoc XML format using Function Module IDOC_XML_TRANSFORM
View iDoc XML format using Function Module IDOC_XML_TRANSFORM


iDoc XML view using Function Module IDOC_XML_TRANSFORM
iDoc XML view using Function Module IDOC_XML_TRANSFORM


Check out my other posts on ALE/iDocs and please leave a comment if you have questions. Cheers!

5 thoughts on “How to Download an iDoc to XML File (3 Different Techniques)

  1. Charlotte Bruce says:

    Hi Ahmed,
    What SAP version are you using to run transaction IDOC. When I run it on ECC6 I only get a list display not ALV so I don’t get options around spreadsheets.

  2. Charlotte Bruce says:

    What version of SAP are you using when you run the IDOC transaction. When running the transaction on our ECC 6 system the list that is generated is a standard list and not an ALV list. I can’t see anywhere to change this.

Leave a Reply

Your email address will not be published. Required fields are marked *