Friday, June 3, 2011

Converting an XML file with many hierarchy levels to ABAP format

This example shows how we can convert an XML file with many hierarchy levels to ABAP format.

Copy and paste the following code in notepad and save it as test.xml file.

Save the XML file which in desktop or some other path

            0000000000038491     ZEU_MATMAS           004     000000000000010003          005      E      promo pack normal      EN           

Here we can see that the root node is ZEU_MATMAS03 which is the main structure containing all other structures.

It contains IDOC structure which in turn contains E1MARAM and EDI_DC40 structures within.

EDI_DC40 contains DOCNUM and MESTYP fields.

E1MARAM contains E1MAKTM structure and MSGFN and MATNR fields.

In SAP we have to create corresponding structures to store this data from XML to ABAP.The main structure contains many deep structures.

So in ABAP dictionary we first create the structure ZIDOC_TEST which contains Structures ZEDIDC40 and Z1EMARAM_TEST1.

ZIDOC_TEST structure contains ZE1MARAM_TEST1 and ZEDIDC40 structures within.

ZEDIDC40 contains DOCNUM and MESTYP fields.

ZE1MARAM_TEST1 contains E1MAKTM structure and MSGFN and MATNR fields.

The structures Z1EMARAM_TEST1 and ZEDIDC40 are also created as shown in the below slides.

This is the structure of ZEDIDC40 - Control record with fields DOCNUM and MESTYP..

The structure ZE1MARAM_TEST1 contains the following fields MSGFN and MATNR.Also a deep structure E1MAKTM.

E1MAKTM Structure is a standard dictionary structure we are using directly with the fields MSGFN,SPRAS,MAKTX and SPRAS_ISO.

The following ABAP program converts the given input XML file to ABAP format.


*Input file contents as string.XML file path where we saved the file. *Here it is saved in desktop.

*Input file with path as constant
CONSTANTS gs_file TYPE string VALUE 'C:\Documents and Settings\Administrator\Desktop\test1.xml'.

*This is the structure type for the data from the XML file
: BEGIN OF ts_zeu_matmas03,
TYPE ZIDOC_TEST, “ZIDOC_TEST structure we created in SE11
END OF ts_zeu_matmas03.

* Table for storing the XML content from file
DATA: gt_itab TYPE STANDARD TABLE OF char2048.

* Table and work areas for the data from the XML file
DATA: gt_zeu_matmas03 TYPE STANDARD TABLE OF ts_zeu_matmas03,
TYPE ts_zeu_matmas03.

* Result table that contains references
* of the internal tables to be filled
DATA: gt_result_xml TYPE abap_trans_resbind_tab,
TYPE abap_trans_resbind.

* For error handling
DATA: gs_rif_ex TYPE REF TO cx_root,
TYPE string.

* Get the XML file from your client
CALL METHOD cl_gui_frontend_services=>gui_upload
filename = gs_file
data_tab = gt_itab
file_open_error =
file_read_error =
no_batch =
gui_refuse_filetransfer =
invalid_type =
no_authority =
unknown_error =
bad_data_format =
header_not_allowed =
separator_not_allowed =
header_too_long =
unknown_dp_error =
access_denied =
dp_out_of_memory =
disk_full =
dp_timeout =
not_supported_by_gui =
error_no_gui =
OTHERS = 19.

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

* Fill the result table with a reference to the data table.
* Within the XSLT style sheet, the data table can be accessed with

GET REFERENCE OF gt_zeu_matmas03 INTO gs_result_xml-value.
gs_result_xml-name =
APPEND gs_result_xml TO gt_result_xml.

* Perform the XSLT stylesheet


SOURCE XML gt_itab
RESULT (gt_result_xml).

In this part of the code

Z_XML_IDOC should be created in XSLT Editor and write code for that as follows:-

We can go there by double clicking on Z_XML_IDOC.Create it and after copying this code activate it.

XSLT is a language is used to convert XML from one format to another format.

Copy paste the following code in XSLT editor :-

Some rules which were applied for conversion to XSLT are:-

IDOC_GET is used to get the data from IDOC.In the XML file the main node is IDOC which has E1MARAM and EDIDC_40 which in turn contains MSGFN,MATNR and E1MAKTM and EDIDC_40 contains MSGFN and DOCNUM respectively.

The same type of structure which is used in our program needs to be filled.In our program EDIDC_40 is used as ZEDIDC40 and E1MARAM is used as ZE1MARAM_TEST1.

The basic template is already filled when we enter the XSLT editor.

We use IDOC_GET to use the IDOC data.We should use the tags to be filled in the hierarchy structure as we declared it in the program.To use the values we need to use the structures in the XML file.Use the corresponding path to access the values.

After transformation the gt_zeu_matmas03 is filled which can be used for displaying.


*Come back to ABAP editor and copy this code for error handling.

CATCH cx_root INTO gs_rif_ex.

gs_var_text = gs_rif_ex->get_text( ).
MESSAGE gs_var_text TYPE 'E'.


*Display the file contents on screen.
sy-subrc = 0.
LOOP at gt_zeu_matmas03 INTO gs_zeu_matmas03.
WRITE: gs_zeu_matmas03-idoc-zedidc40-docnum, “document no
/ gs_zeu_matmas03-idoc-zedidc40-mestyp, “message type
/ gs_zeu_matmas03-idoc-ze1maram_test1-msgfn,”message function
/ gs_zeu_matmas03-idoc-ze1maram_test1-matnr,”material no

*all fields of E1MAKTM segment
/ gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-msgfn,
/ gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-spras,
/ gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-maktx,
/ gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-spras_iso.

We can see the values filled in the heirarchy structure of IDOC as follows:-

Execute the ABAP program and we get the file contents displayed as below.So we have converted the given XML File to a structure and display the contents.

Final output of the program is:-

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)


Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials