Vous êtes sur la page 1sur 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.
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 stringVALUE 'C:\Documents and Settings\Administrator\Desktop\tes
t1.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).
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.
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output
encoding="iso-8859-1"
indent="yes"
method="xml"
version="1.0"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<IDOC_GET>
<xsl:apply-templates select="//IDOC"/>
</IDOC_GET>
</asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="IDOC">
<item>
<IDOC>
<ZEDIDC40>
<MESTYP><xsl:value-of select="EDI_DC40/MESTYP"/></MESTYP>
<DOCNUM><xsl:value-of select="EDI_DC40/DOCNUM"/></DOCNUM>
</ZEDIDC40>
<ZE1MARAM_TEST1>
<MSGFN><xsl:value-of select="E1MARAM/MSGFN"/></MSGFN>
<MATNR><xsl:value-of select="E1MARAM/MATNR"/></MATNR>
<E1MAKTM>
<MSGFN><xsl:value-of
select="E1MARAM/E1MAKTM/MSGFN"/></MSGFN>
<SPRAS><xsl:value-of
select="E1MARAM/E1MAKTM/SPRAS"/></SPRAS>
<MAKTX><xsl:value-of
select="E1MARAM/E1MAKTM/MAKTX"/></MAKTX>
<SPRAS_ISO><xsl:value-of
select="E1MARAM/E1MAKTM/SPRAS_ISO"/></SPRAS_ISO>
</E1MAKTM>
</ZE1MARAM_TEST1>
</IDOC>
</item>
</xsl:template>
</xsl:transform>

*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'.
ENDTRY.
*Display the file contents on screen.
IF 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.
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:

SAP Transformation Tcodes


STRANS - Start transformation Tool Basis - Workbench Tools: Editors, Painters, Modelers
XSLT - XSLT tester Basis - ABAP XML Processing
RSDS - DataSource BW - Data Staging
SXMB_MONI_BPE - Process Engine - Monitoring Basis - Runtime Workbench/Monitoring
RSMONITOR - Generic Monitor BW - Data Basis
RSCUR - Start: Currency Translation Type BW - OLAP Technology
GLPCP - Display Table GLPCP Enterprice Controlling - Profit Center Accounting
SMT - Customizing transformation Workbench Cross Application - Service Mapping Tool

XSLT_TOOL - Start XSLT Tool Basis - Workbench Tools: Editors, Painters, Modelers
EA60 - Print Invoicing Document IS - Invoicing
PPMM - Personnel Planning Basis - Organizational Management
CADE - CATS: Delete Transaction Data Cross Application - Time Sheet
MRTRS_START - RMS-MRTRS : Start MR transformation Product Lifecycle Management
SSTDEMO1 - Simple transformations Demo: Flights Basis - XML Generic Technology
CRMC_ERMS_SM_SRV - Define Services CRM - Rule Maintenance
RSTRANGUI - transformations BW - Data Staging
WUFWUF - Customizing transformation Workbench

Vous aimerez peut-être aussi