Converting an XML file with many hierarchy levels to ABAP format

By Subhashini Kolluri, IBM India

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  

 <?xml version="1.0" encoding="UTF-8" ?> 
<ZEU_MATMAS03>
 <IDOC BEGIN="1">
  <EDI_DC40 SEGMENT="1">
   <DOCNUM>0000000000038491</DOCNUM> 
   <MESTYP>ZEU_MATMAS</MESTYP> 
  </EDI_DC40>
  <E1MARAM SEGMENT="1">
   <MSGFN>004</MSGFN> 
   <MATNR>000000000000010003</MATNR> 
   <E1MAKTM SEGMENT="1">
    <MSGFN>005</MSGFN> 
    <SPRAS>E</SPRAS> 
    <MAKTX>promo pack normal</MAKTX> 
    <SPRAS_ISO>EN</SPRAS_ISO> 
   </E1MAKTM>
  </E1MARAM>
 </IDOC>
</ZEU_MATMAS03>

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.  

TYPE-POOLS abap.

*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
TYPES
BEGIN OF ts_zeu_matmas03,
         idoc 
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,
      gs_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,
      gs_result_xml 
TYPE abap_trans_resbind.

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

* Get the XML file from your client
CALL METHOD cl_gui_frontend_services=>gui_upload
  
EXPORTING
    filename                = gs_file
  
CHANGING
    data_tab                = gt_itab
  
EXCEPTIONS
    file_open_error         = 
1
    file_read_error         = 
2
    no_batch                = 
3
    gui_refuse_filetransfer = 
4
    invalid_type            = 
5
    no_authority            = 
6
    unknown_error           = 
7
    bad_data_format         = 
8
    header_not_allowed      = 
9
    separator_not_allowed   = 
10
    header_too_long         = 
11
    unknown_dp_error        = 
12
    access_denied           = 
13
    dp_out_of_memory        = 
14
    disk_full               = 
15
    dp_timeout              = 
16
    not_supported_by_gui    = 
17
    error_no_gui            = 
18
    
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.
ENDIF.

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

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

* Perform the XSLT stylesheet

TRY.

    
CALL TRANSFORMATION z_xml_idoc
    SOURCE XML gt_itab
    RESULT (gt_result_xml).

Click here to continue...

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image