Posted: Wed Apr 23, 2008 5:18 pm Post subject: Tran. of internal table into XML and creating a ZIP File
Dynamic Transformation of Internal table data into XML and Creating a ZIP File
Original: https: //wiki.sdn.sap.com/wiki/display/Snippets/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
Code:
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 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 :
<gt_data> TYPE table,
<wa> 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 : <gt_data>,<wa>.
ASSIGN lwa_data-data->* TO <gt_data>.
CREATE DATA data_line LIKE LINE OF <gt_data>.
ASSIGN data_line->* TO <wa>.
*Create the XML Transformation
CALL TRANSFORMATION id
SOURCE <wa> = <gt_data>
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.
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 : <gt_data>,<wa>.
ASSIGN lwa_file-data->* TO <gt_data>.
CREATE DATA data_line LIKE LINE OF <gt_data>.
ASSIGN data_line->* TO <wa>.
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 <wa> = <gt_data>.
MODIFY it_data FROM lwa_file INDEX sy-tabix.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
3. sample program
Code:
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.
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.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.