Convert IDoc to an other format can common business requirement in order to make communication possible between SAP System and other Landspace.
In this article, you will learn how to convert IDoc to XML file having the same schema as the original IDoc.
First, how to retrieve SAP IDOC Schema, then the different steps to make the IDoc Conversion possible. Finally, you will find the full SAP ABAP Program Source ready to go !
Table of Contents
How to retrieve SAP IDOC Schema
Before convert Idoc to XML format in SAP ABAP, you have to generate the Schema.
Actually, the SAP IDoc Schema can be generated by the tcode WE60 as following
Steps Convert Idoc To XML
Here the different steps to enable Convert SAP IDoc to XML File using ABAP code.
Retrieve list of Idocs ( Table EDIDC )
Build XML Filename ->?: [Path] \ [Idoc_Nb] [.xml]
Check if file exists: if the document already exists, Skip this SAP Idoc
This standard method check if a local file exist CL_GUI_FRONTEND_SERVICE=>FILE_EXIST.
CALL METHOD cl_gui_frontend_services=>file_exist EXPORTING file = lv_datfilename RECEIVING result = lv_result EXCEPTIONS cntl_error = 1 error_no_gui = 2 wrong_parameter = 3 not_supported_by_gui = 4 OTHERS = 5.
Create IDoc object using the cl_idoc_xml1 class
The key here to convert IDoc to XML file is the standard calss CL_IDOC_XML1.
As input, you should give it the Idoc number. An IDoc’s XML Object is automatically generated.
CREATE OBJECT lo_idoc_xml EXPORTING docnum =-docnum EXCEPTIONS OTHERS = 1.
Extract Idocs Data as String
Once, the Idoc’s Object is built, we will use lo_idoc_xml->get_xmldata_as_string in order to retrieve the XML data as string.
CALL METHOD lo_idoc_xml->get_xmldata_as_string IMPORTING data_string = gv_string.
Download IDoc data as XML file
Once the data is ready, it is time to download the XML to a local folder.
The Standard Function module GUI_DOWNLOAD fits the need here.
CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = gv_filename TABLES data_tab = gt_string
The complete ABAP program to Download IDoc in XML
Once you understand how it works, feel free to copy and test this ABAP Report.
Caution: use it as it. I am not responsable on the potentiel issue.?
*&---------------------------------------------------------------------* *& Report ZCONVERT_IDOC_TO_XML *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT zcrm_convert_idoc_to_xml. TABLES: edidc. DATA: lv_docnum TYPE string. DATA: lo_idoc_xml TYPE REF TO cl_idoc_xml1. DATA: gv_string TYPE string. DATA: gt_string TYPE TABLE OF string. DATA: gv_filename TYPE string. DATA: lv_result TYPE c. DATA: lv_datfilename TYPE string. DATA: lv_folder TYPE string. DATA: lt_edidc TYPE TABLE OF edidc. FIELD-SYMBOLS:LIKE LINE OF lt_edidc. " Selection Screen SELECT-OPTIONS: s_idoc FOR edidc-docnum. SELECT-OPTIONS: s_stat FOR edidc-status. SELECT-OPTIONS: s_cred FOR edidc-credat. SELECT-OPTIONS: s_cret FOR edidc-cretim. PARAMETERS: p_mestyp TYPE edidc-mestyp. PARAMETERS: p_idoct TYPE edidc-idoctp. PARAMETERS: p_rcvprn TYPE edidc-rcvprn DEFAULT 'RECPRN'. PARAMETERS: p_path LIKE rlgrap-filename DEFAULT 'C:\'. " Help for Folder Selection AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path. CALL METHOD cl_gui_frontend_services=>directory_browse EXPORTING window_title = 'Choisir dossier' initial_folder = 'C:' CHANGING selected_folder = lv_folder. p_path = lv_folder . CALL METHOD cl_gui_cfw=>flush. " Begin Processing START-OF-SELECTION. REFRESH lt_edidc[]. " Retrieve list of Idocs SELECT * FROM edidc INTO TABLE lt_edidc WHERE docnum IN s_idoc[] AND status IN s_stat[] AND mestyp EQ p_mestyp AND idoctp EQ p_idoct AND credat IN s_cred[] AND cretim IN s_cret[]. LOOP AT lt_edidc ASSIGNING . " Clear and Init for each Idoc CLEAR: gv_string, gv_filename, lv_docnum. CLEAR lo_idoc_xml. REFRESH gt_string. " Remove Leading 0 in Idoc Number lv_docnum = -docnum. SHIFT lv_docnum LEFT DELETING LEADING '0'. " Build XML Filename : Path \ Idoc_Nb .XML CONCATENATE p_path '\' -mestyp '_' lv_docnum INTO gv_filename. REPLACE ALL OCCURRENCES OF '\\' IN gv_filename WITH '\'. CONCATENATE gv_filename '.xml' INTO gv_filename. " Check if file exists -> skip this Idoc CALL METHOD cl_gui_frontend_services=>file_exist EXPORTING file = lv_datfilename RECEIVING result = lv_result EXCEPTIONS cntl_error = 1 error_no_gui = 2 wrong_parameter = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy-subrc <> 0. CONTINUE. ENDIF. " Create IDoc object CREATE OBJECT lo_idoc_xml EXPORTING docnum = -docnum EXCEPTIONS OTHERS = 1. IF sy-subrc NE 0. WRITE: /'Error creating idoc object'. CONTINUE. ENDIF. " Get IDoc data as string CALL METHOD lo_idoc_xml->get_xmldata_as_string IMPORTING data_string = gv_string. APPEND gv_string TO gt_string. IF sy-subrc NE 0 OR lo_idoc_xml IS INITIAL. WRITE: /'Error getting xml data as string for idoc ' , lv_docnum. CONTINUE. ENDIF. " Download IDoc data as XML file CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = gv_filename TABLES data_tab = gt_string EXCEPTIONS 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. WRITE: /'Error saving idoc as XML file for idoc' , lv_docnum. CONTINUE. ENDIF. REFRESH gt_string[]. ENDLOOP.
Find also an other SAP Scn Wiki how to convert SAP IDoc to XML file.