sap download idoc data to xml

 If you want to download idoc data to XML, 
you can use below mention code. This will take IDOC number as input, first convert that to XML data as string, then append into a table, POP up given to get the save location where the file to be stored, and then finally idoc data is stored as a file in XML format.




REPORT ZK_IDOC_TO_XML.

"this program can be use if you want to downlaod idoc data into xml file

*Global tables
DATAgt_data_tab TYPE TABLE OF string WITH HEADER LINE.     "download data tab

*Global variables
DATAgv_filename TYPE string,                               "file name
      gv_path     TYPE string,                               "file path
      gv_fullpath     TYPE string,                           "file full path
      gv_result   TYPE i,
      LV_BIN_FILESIZE TYPE I" Binary File Size
      gv_xml_str TYPE string,                                "XML string
      gv_url(500).

DATAlcl_idoc TYPE REF TO cl_idoc_xml1.   "

*Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
PARAMETERSp_docnum LIKE edidc-docnum OBLIGATORY.            "iDoc number
SELECTION-SCREEN END OF BLOCK b2.

START-OF-SELECTION.
"convert idoc data into xml format using class
     perform idoc_to_xml.
"give pop up to user and get location where file to be saved
     perform get_save_location.
"save the file at the given location and execute (open it)
     perform download.


end-of-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  IDOC_TO_XML
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM IDOC_TO_XML.

* Create Object and create IDOC_XML
  CREATE OBJECT lcl_idoc
    EXPORTING
      docnum             p_docnum
    EXCEPTIONS
      error_loading_idoc 1
      error_building_xml 2
      OTHERS             3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
               RAISING no_idoc_xml_loaded.
  ENDIF.

* Transformation
  CALL METHOD lcl_idoc->get_xmldata_as_string
    IMPORTING
      data_string gv_xml_str.

*append xml string to data tab
  APPEND gv_xml_str TO gt_data_tab.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_SAVE_LOCATION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_SAVE_LOCATION.

  CONCATENATE 'IDOC_' P_DOCNUM INTO gv_filename.

* Display save dialog window
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title      'Select the location'
default_extension 'xml'
default_file_name GV_FILENAME
*initial_directory = ‘C:’
CHANGING
filename          gv_filename
path              gv_path
fullpath          GV_FULLPATH
user_action       gv_result.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DOWNLOAD.

    if GV_FULLPATH is NOT INITIAL.

      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
*          BIN_FILESIZE            = LV_BIN_FILESIZE
          FILENAME                GV_FULLPATH
*          FILETYPE                = 'BIN'
* IMPORTING
*         FILELENGTH              =
        TABLES
          DATA_TAB                GT_DATA_TAB
*         FIELDNAMES              =
        EXCEPTIONS
          FILE_WRITE_ERROR        1
          NO_BATCH                2
          GUI_REFUSE_FILETRANSFER 3
          INVALID_TYPE            4
          NO_AUTHORITY            5
          UNKNOWN_ERROR           6
          HEADER_NOT_ALLOWED      7
          SEPARATOR_NOT_ALLOWED   8
          FILESIZE_NOT_ALLOWED    9
          HEADER_TOO_LONG         10
          DP_ERROR_CREATE         11
          DP_ERROR_SEND           12
          DP_ERROR_WRITE          13
          UNKNOWN_DP_ERROR        14
          ACCESS_DENIED           15
          DP_OUT_OF_MEMORY        16
          DISK_FULL               17
          DP_TIMEOUT              18
          FILE_NOT_FOUND          19
          DATAPROVIDER_EXCEPTION  20
          CONTROL_FLUSH_ERROR     21
          OTHERS                  22.
            .
    IF SY-SUBRC <> 0.
*     Implement suitable error handling here

      else.  "execute file

      CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE
        EXPORTING
          DOCUMENT               GV_FULLPATH
*          APPLICATION            =
*          PARAMETER              =
*          DEFAULT_DIRECTORY      =
*          MAXIMIZED              =
*          MINIMIZED              =
*          SYNCHRONOUS            =
*          OPERATION              = 'OPEN'
        EXCEPTIONS
          CNTL_ERROR             1
          ERROR_NO_GUI           2
          BAD_PARAMETER          3
          FILE_NOT_FOUND         4
          PATH_NOT_FOUND         5
          FILE_EXTENSION_UNKNOWN 6
          ERROR_EXECUTE_FAILED   7
          SYNCHRONOUS_FAILED     8
          NOT_SUPPORTED_BY_GUI   9
          OTHERS                 10
              .
      IF SY-SUBRC <> 0.
*       Implement suitable error handling here
      ENDIF.


      ENDIF.

    endif.

ENDFORM.











Post a Comment

0 Comments

Total Pageviews