SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Tran. of internal table into XML and creating a ZIP File



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Wed Apr 23, 2008 5:18 pm    Post subject: Tran. of internal table into XML and creating a ZIP File Reply with quote

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
Code:
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 :
  <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.

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

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
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.