Google Search - Blog...........

ABAP - Dynamic Transformation of Internal table data into XML and Creating a ZIP File

In this code tutorial,I would like to explain how the data from any internal table can be zipped and subsequently converted into a downloadable XML file and visa versa.

1. Creation of Type Pool ZDATA

TYPE-POOL zdata .
TYPES:
BEGIN OF zdata_t_splice_entry,
name TYPE string,
offset TYPE i,
length TYPE i,
compressed TYPE i,
END OF zdata_t_splice_entry .
TYPES:
zdata_t_splice_entries TYPE STANDARD TABLE OF zdata_t_splice_entry WITH DEFAULT KEY ,
BEGIN OF zdata_t_data,
data TYPE REF TO data,
xmlfile TYPE string,
END OF zdata_t_data,
zdata_t_data_entries type standard table of zdata_t_data.

2. Creation of Function Module ZDOWNLOAD_AND_ZIP_TO_XML

FUNCTION-POOL ZXML. "MESSAGE-ID ..
type-pools : ZDATA.
FUNCTION zdownload_and_zip_to_xml.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(FULLPATH) TYPE STRING
*" REFERENCE(DOWNLOAD) TYPE XFELD DEFAULT 'X'
*" REFERENCE(UPLOAD) TYPE XFELD OPTIONAL
*" TABLES
*" IT_DATA TYPE ZDATA_T_DATA_ENTRIES
*" EXCEPTIONS
*" FILE_NOT_CREATED
*"----------------------------------------------------------------------
DATA :
data_line TYPE REF TO data,
ixml TYPE xstring,
xml TYPE string,
izip TYPE REF TO cl_abap_zip,
zip_file TYPE xstring,
binary_tab TYPE STANDARD TABLE OF x255,
lwa_data TYPE zdata_t_data,
lt_splice_entries TYPE TABLE OF zdata_t_splice_entry,
lwa_splice_entries TYPE zdata_t_splice_entry,
l_filename TYPE string,
input_length TYPE i,
lv_index TYPE sytabix,
lwa_file TYPE zdata_t_data.
FIELD-SYMBOLS :
TYPE table,
TYPE ANY.
CREATE OBJECT izip.
IF download = 'X'.
*Download to XML File(s)
LOOP AT it_data INTO lwa_data.
CLEAR : data_line,zip_file,binary_tab[],ixml,xml.
UNASSIGN : ,.
ASSIGN lwa_data-data->* TO .
CREATE DATA data_line LIKE LINE OF .
ASSIGN data_line->* TO .
*Create the XML Transformation
CALL TRANSFORMATION id
SOURCE =
RESULT XML ixml.
izip->add( name = lwa_data-xmlfile
content = ixml ).
ENDLOOP.
*Create a new zip file
zip_file = izip->save( ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = zip_file
TABLES
binary_tab = binary_tab.
cl_gui_frontend_services=>gui_download(
EXPORTING
filename = fullpath
filetype = 'BIN'
CHANGING
data_tab = binary_tab ).
IF sy-subrc NE 0.
RAISE file_not_created.
ENDIF.
ELSE.
*Upload from XML File(s)
l_filename = fullpath.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filetype = 'BIN'
filename = l_filename
IMPORTING
filelength = input_length
CHANGING
data_tab = binary_tab.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = input_length
IMPORTING
buffer = zip_file
TABLES
binary_tab = binary_tab.
CHECK binary_tab IS NOT INITIAL.
CLEAR : lt_splice_entries.
CALL METHOD cl_abap_zip=>splice
EXPORTING
zip = zip_file
RECEIVING
entries = lt_splice_entries.
izip->load( zip_file ).
IF LINES( lt_splice_entries ) GT 0.
LOOP AT lt_splice_entries INTO lwa_splice_entries.
lv_index = sy-tabix.
CLEAR : lwa_file.
READ TABLE it_data INTO lwa_file
WITH KEY xmlfile = lwa_splice_entries-name.
IF sy-subrc EQ 0.
CLEAR : data_line,ixml.
UNASSIGN : ,.
ASSIGN lwa_file-data->* TO .
CREATE DATA data_line LIKE LINE OF .
ASSIGN data_line->* TO .
CALL METHOD izip->get
EXPORTING
name = lwa_splice_entries-name
IMPORTING
content = ixml
EXCEPTIONS
zip_index_error = 1
zip_decompression_error = 2
OTHERS = 3.
CALL TRANSFORMATION id
SOURCE XML ixml
RESULT = .
MODIFY it_data FROM lwa_file INDEX sy-tabix.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
3. SAMPLE PROGRAM
REPORT zzip_xml.
TYPE-POOLS : zdata.
TYPES:
BEGIN OF t_makt,
counter TYPE i.
INCLUDE TYPE makt.
TYPES : END OF t_makt,
BEGIN OF t_tvkot,
counter TYPE i.
INCLUDE TYPE tvkot.
TYPES : END OF t_tvkot.
TYPES:
BEGIN OF t_splice_entry,
name TYPE string,
offset TYPE i,
length TYPE i,
compressed TYPE i,
END OF t_splice_entry .
TYPES:
t_splice_entries TYPE STANDARD TABLE OF t_splice_entry WITH DEFAULT KEY .
PARAMETERS : p_path TYPE string OBLIGATORY DEFAULT 'C:\',
p_down RADIOBUTTON GROUP r1 ,
p_up RADIOBUTTON GROUP r1.
DATA : stripped_name TYPE string,
file_path TYPE string,
gt_data TYPE zdata_t_data_entries,
wa_data TYPE zdata_t_data.
DATA retfiletable TYPE filetable.
DATA retrc TYPE sysubrc.
DATA retuseraction TYPE i.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
IF p_down = 'X'.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = 'Downloaded ZIP File'
file_filter = '*.zip'
default_extension = 'zip'
CHANGING
filename = stripped_name
path = file_path
fullpath = p_path
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
ELSE.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
multiselection = abap_false
file_filter = '*.zip'
default_extension = 'zip'
CHANGING
file_table = retfiletable
rc = retrc
user_action = retuseraction.
READ TABLE retfiletable INTO p_path INDEX 1.
ENDIF.
START-OF-SELECTION.
IF p_down = 'X'.
*Download to XML File
PERFORM create_zip_and_xml_files.
ELSE.
*Upload from XML File
PERFORM splice_xml_files.
ENDIF.
*&---------------------------------------------------------------------*
*& Form create_zip_and_xml_files
*&---------------------------------------------------------------------*
FORM create_zip_and_xml_files .
DATA :
lt_data TYPE STANDARD TABLE OF t_makt,
lwa_data TYPE t_makt,
lt_tvkot TYPE STANDARD TABLE OF t_tvkot,
lwa_tvkot TYPE t_tvkot,
lv_index TYPE sy-index,
lt_outtab TYPE REF TO data,
lt_tvkot_outtab TYPE REF TO data,
lwa_file TYPE zdata_t_data.
*Select some MAKT data
SELECT * FROM makt INTO CORRESPONDING FIELDS OF
TABLE lt_data UP TO 20 ROWS
WHERE spras = 'EN'.
LOOP AT lt_data INTO lwa_data.
lv_index = sy-tabix.
lwa_data-counter = lv_index.
MODIFY lt_data FROM lwa_data INDEX lv_index.
ENDLOOP.
IF LINES( lt_data ) GT 0.
*Get the internal table into data area
GET REFERENCE OF lt_data INTO lt_outtab.
ENDIF.
*Select some TVKO data
CLEAR : lv_index.
SELECT * FROM tvkot INTO CORRESPONDING FIELDS OF
TABLE lt_tvkot UP TO 20 ROWS
WHERE spras = 'EN'.
LOOP AT lt_tvkot INTO lwa_tvkot.
lv_index = sy-tabix.
lwa_tvkot-counter = lv_index.
MODIFY lt_tvkot FROM lwa_tvkot INDEX lv_index.
ENDLOOP.
IF LINES( lt_tvkot ) GT 0.
*Get the internal table into data area
GET REFERENCE OF lt_tvkot INTO lt_tvkot_outtab.
ENDIF.
*Add to data table
lwa_file-data = lt_outtab.
lwa_file-xmlfile = 'MAKT.XML'.
APPEND lwa_file TO gt_data.
lwa_file-data = lt_tvkot_outtab.
lwa_file-xmlfile = 'TVKOT.XML'.
APPEND lwa_file TO gt_data.
.
CALL FUNCTION 'ZDOWNLOAD_AND_ZIP_TO_XML'
EXPORTING
fullpath = p_path
download = 'X'
* UPLOAD =
TABLES
it_data = gt_data
EXCEPTIONS
file_not_created = 1
OTHERS = 2
.
ENDFORM. " create_zip_and_xml_files
*&---------------------------------------------------------------------*
*& Form splice_xml_files
*&---------------------------------------------------------------------*
FORM splice_xml_files .
DATA binary_tab TYPE STANDARD TABLE OF x255.
DATA ixml TYPE xstring.
DATA izip TYPE REF TO cl_abap_zip.
DATA zip_file TYPE xstring.
DATA input_length TYPE i.
DATA : lt_splice_entries TYPE TABLE OF t_splice_entry,
lwa_splice_entries TYPE t_splice_entry,
lwa_file TYPE zdata_t_data,
lt_data TYPE STANDARD TABLE OF t_makt,
lt_tvkot TYPE STANDARD TABLE OF t_tvkot,
lt_outtab TYPE REF TO data,
lt_tvkot_outtab TYPE REF TO data,
data_line TYPE REF TO data,
lv_index TYPE sytabix,
lwa_data TYPE t_makt,
lwa_tvkot TYPE t_tvkot.
CLEAR gt_data.
*Get the internal table into data area
GET REFERENCE OF lt_data INTO lt_outtab.
lwa_file-data = lt_outtab.
lwa_file-xmlfile = 'MAKT.XML'.
APPEND lwa_file TO gt_data.
CLEAR lwa_file.
GET REFERENCE OF lt_tvkot INTO lt_tvkot_outtab.
lwa_file-data = lt_tvkot_outtab.
lwa_file-xmlfile = 'TVKOT.XML'.
APPEND lwa_file TO gt_data.
CLEAR lwa_file.
CALL FUNCTION 'ZDOWNLOAD_AND_ZIP_TO_XML'
EXPORTING
fullpath = p_path
download = space
upload = 'X'
TABLES
it_data = gt_data
EXCEPTIONS
file_not_created = 1
OTHERS = 2.
WRITE : 'MAKT Data--------------'.
LOOP AT lt_data INTO lwa_data.
WRITE : / lwa_data-matnr.
ENDLOOP.
SKIP 5.
WRITE : / 'TVKO Data--------------'.
LOOP AT lt_tvkot INTO lwa_tvkot.
WRITE : / lwa_tvkot-vkorg.
ENDLOOP.
ENDFORM. " splice_xml_files

1 comment:

  1. has syntax errors:
    UNASSIGN : ,.
    ASSIGN lwa_data-data->* TO .

    ReplyDelete