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

Converting an XML File With Many Hierarchy Levels to ABAP Format

Embed Size (px)

DESCRIPTION

Converting an XML File With Many Hierarchy Levels to ABAP Format

Citation preview

Converting an XML file with many hierarchy levels to ABAP format

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

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. TYPE-POOLSabap. *Input file contents as string.XML file path where we saved the file. *Here it is saved in desktop. *Input file with path as constantCONSTANTSgs_fileTYPEstring VALUE'C:\DocumentsandSettings\Administrator\Desktop\test1.xml'.

*Thisisthestructuretype forthedatafromtheXMLfileTYPES:BEGINOFts_zeu_matmas03,idocTYPEZIDOC_TEST, ZIDOC_TEST structure we created in SE11ENDOFts_zeu_matmas03.

*Tableforstoring theXMLcontent from fileDATA:gt_itabTYPESTANDARDTABLEOFchar2048.

*TableandworkareasforthedatafromtheXMLfileDATA:gt_zeu_matmas03TYPESTANDARDTABLEOFts_zeu_matmas03,gs_zeu_matmas03TYPEts_zeu_matmas03.

*Resulttablethatcontainsreferences*oftheinternaltablestobefilledDATA:gt_result_xmlTYPEabap_trans_resbind_tab,gs_result_xmlTYPEabap_trans_resbind.

*ForerrorhandlingDATA:gs_rif_exTYPEREFTOcx_root,gs_var_textTYPEstring.

*GettheXMLfilefromyourclientCALLMETHODcl_gui_frontend_services=>gui_uploadEXPORTINGfilename=gs_fileCHANGINGdata_tab=gt_itabEXCEPTIONSfile_open_error=1file_read_error=2no_batch=3gui_refuse_filetransfer=4invalid_type=5no_authority=6unknown_error=7bad_data_format=8header_not_allowed=9separator_not_allowed=10header_too_long=11unknown_dp_error=12access_denied=13dp_out_of_memory=14disk_full=15dp_timeout=16not_supported_by_gui=17error_no_gui=18OTHERS=19.

IFsy-subrc0.MESSAGEIDsy-msgidTYPEsy-msgtyNUMBERsy-msgnoWITHsy-msgv1sy-msgv2sy-msgv3sy-msgv4.ENDIF.

*Filltheresulttablewithareferencetothedatatable.*WithintheXSLTstyle sheet,thedatatablecanbeaccessedwith*"IDOC_GET".

GETREFERENCEOFgt_zeu_matmas03INTOgs_result_xml-value.gs_result_xml-name='IDOC_GET'.APPENDgs_result_xmlTOgt_result_xml.

*PerformtheXSLTstylesheet

TRY.

CALLTRANSFORMATIONz_xml_idocSOURCEXMLgt_itabRESULT(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. CATCHcx_rootINTOgs_rif_ex.

gs_var_text=gs_rif_ex->get_text().MESSAGEgs_var_textTYPE'E'.

ENDTRY. *Display the file contents on screen.IFsy-subrc=0.LOOPatgt_zeu_matmas03INTOgs_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.ENDLOOP.ENDIF. 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:-