Convert IDOC to XML format with Full ABAP Code

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 !

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
WE60 - IDoc XML Schema

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.

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.

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.

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.

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.?

Find also an other SAP Scn Wiki how to convert SAP IDoc to XML file.