SAP创建XML 文件
阅读原文时间:2023年07月08日阅读:3

TYPES: BEGIN OF xml_line_type,
data(256) TYPE x,
END OF xml_line_type,
xml_tab_type TYPE TABLE OF xml_line_type,
xml_element TYPE REF TO if_ixml_element.

DATA:unicodelg LIKE x030l-unicodelg,
temp_fore_filename TYPE string,
curr_object_type TYPE i,
percentage TYPE p DECIMALS 2,
obj_perc TYPE p DECIMALS 2,
temp_back_filename LIKE rlgrap-filename,
g_iref_root_elem TYPE REF TO if_ixml_element,
g_iref_pixml TYPE REF TO if_ixml,
g_iref_pdocument TYPE REF TO if_ixml_document,
g_iref_pstreamfactory TYPE REF TO if_ixml_stream_factory,
gt_converted_data TYPE xml_tab_type,
g_ostream TYPE REF TO if_ixml_ostream,
g_buffer_itab TYPE xml_tab_type,
g_strx TYPE xstring,
gv_tabix TYPE sy-tabix,
g_file_append_mode TYPE char01 VALUE space.

DATA:p_fore TYPE char128 VALUE 'C:\Users\wangz94\Desktop\xml\xml.xml',
p_back TYPE rlgrap-filename,
r_fore TYPE c VALUE 'X',
r_back TYPE c VALUE '',
no_usage TYPE c,
popup_ok TYPE c VALUE 'X',
new_line TYPE string.

CONSTANTS: encoding(10) TYPE c VALUE 'UTF-8',
num_object_types TYPE i VALUE 48,
* Test mode definiton - do not change
panaya_test_mode TYPE c VALUE space,
show_servers_name TYPE c VALUE 'X',
skip_empty_tags TYPE c VALUE 'X',
buffer_max_size TYPE i VALUE 10000000,
general_part_ver(15) TYPE c VALUE '2.36',
rel_specific_ver(15) TYPE c VALUE '10_2005',
root_element_name(30) TYPE c VALUE 'ROOT_ELEMENT'.

temp_fore_filename = p_fore.
* Create the main factory and the initial document
PERFORM create_xml_document.
* Create the root element of the XML
PERFORM xml_header USING g_iref_pdocument root_element_name
CHANGING g_iref_root_elem.

PERFORM open_xml_file.
* Add data to the XML
PERFORM add_data_to_xml.

PERFORM close_xml_file.

FORM add_data_to_xml.
PERFORM add_document_header.
ENDFORM.
*---------------------------------------------------------------------*
* FORM create_xml_document 创建XML文件 *
*---------------------------------------------------------------------*
FORM create_xml_document.

DATA: l_encoding TYPE string,
i_encoding TYPE REF TO if_ixml_encoding.

* create the main factory
g_iref_pixml = cl_ixml=>create( ).

* create the initial document
g_iref_pdocument = g_iref_pixml->create_document( ).

l_encoding = encoding.
i_encoding = g_iref_pixml->create_encoding( character_set = l_encoding
byte_order = '0' ).
CALL METHOD g_iref_pdocument->set_encoding( encoding = i_encoding ).

ENDFORM. " create_xml_document

*---------------------------------------------------------------------*
* FORM xml_header(XML 头部信息) *
*---------------------------------------------------------------------*
FORM xml_header USING VALUE(pi_pdocument) TYPE REF TO if_ixml_document
VALUE(pi_xml_root) TYPE c
CHANGING VALUE(pc_elem) TYPE REF TO if_ixml_element.

DATA:
l_simple_elem TYPE REF TO if_ixml_element,
l_elem TYPE REF TO if_ixml_element,
l_result TYPE i,
l_name TYPE string.

l_name = pi_xml_root .
l_elem = pi_pdocument->create_element( name = l_name ).
l_result = pi_pdocument->append_child( l_elem ).
pc_elem = l_elem.

ENDFORM. " xml_header

*---------------------------------------------------------------------*
* FORM add_attribute_to_node (XML节点属性) *
*---------------------------------------------------------------------*
FORM add_attribute_to_node USING
VALUE(pi_elem) TYPE REF TO if_ixml_element
VALUE(pi_attr_name) TYPE string
VALUE(pi_value) TYPE any.

DATA: l_value TYPE string.

l_value = pi_value.

IF l_value IS INITIAL AND NOT skip_empty_tags IS INITIAL.
EXIT.
ENDIF.

CALL METHOD pi_elem->set_attribute
EXPORTING
name = pi_attr_name
value = l_value.

ENDFORM. "add_attribute_to_node

*&---------------------------------------------------------------------*
*& Form add_document_header
*&---------------------------------------------------------------------*
FORM add_document_header.

DATA: header_node TYPE xml_element.
DATA: code_version(15) TYPE c.

DATA: lv_text TYPE char10,
lv_license TYPE char10,
lv_mode TYPE char10,
lv_logsys TYPE logsys,
lv_rfc_sysid TYPE sy-sysid,
lv_rfc_mandt TYPE sy-mandt.

DATA: k_release TYPE sysaprl,
k_patch TYPE sychar05.
DATA: host TYPE string.

* Create a child node for the Header node
PERFORM create_xml_node USING g_iref_pdocument g_iref_root_elem 'HEADER'
CHANGING header_node.
* Set Date
lv_text = sy-datum.
PERFORM add_attribute_to_node USING header_node 'DATE' lv_text.
* Set Time
lv_text = sy-uzeit.
PERFORM add_attribute_to_node USING header_node 'TIME' lv_text.
* Set Application Server
IF show_servers_name IS INITIAL.
host = 'XXX'.
ELSE.
host = sy-host.
ENDIF.
PERFORM add_attribute_to_node USING header_node 'APPLICATION_SERVER' host.
* Set System ID name
PERFORM add_attribute_to_node USING header_node 'SYSTEM_ID' sy-sysid.
* Set Client number
PERFORM add_attribute_to_node USING header_node 'CLIENT' sy-mandt.
* Set Operating System
PERFORM add_attribute_to_node USING header_node 'OPERATING_SYSTEM' sy-opsys.
* Set SAP System Release
PERFORM add_attribute_to_node USING header_node 'SYSTEM_RELEASE' sy-saprl.
* Set Installation number
CALL FUNCTION 'SLIC_GET_LICENCE_NUMBER'
IMPORTING
license_number = lv_license.
PERFORM add_attribute_to_node USING header_node 'INSTALLATION_NUMBER' lv_license.
* Set User name
PERFORM add_attribute_to_node USING header_node 'USER_NAME' sy-uname.
* Set Export Tool Version
CONCATENATE general_part_ver rel_specific_ver INTO code_version SEPARATED BY '.'.
PERFORM add_attribute_to_node USING header_node 'EXPORT_TOOL_VERSION' code_version.
* Set Target System
PERFORM add_attribute_to_node USING header_node 'TARGET_SYSTEM' 'DR1'.
* Set Target System Id ( production system id )
PERFORM add_attribute_to_node USING header_node 'TARGET_SYSTEM_ID' 'rfc_sysid'.
* Set Target system release
PERFORM add_attribute_to_node USING header_node 'TARGET_SYSTEM_REL' 'rfc_saprl'.
* Set ETL vanilla extraction flag
PERFORM add_attribute_to_node USING header_node 'VANILLA_EXTRACTION' 'X'.
* Set SM Target System
PERFORM add_attribute_to_node USING header_node 'SM_SYSTEM' 'p_sm_rfc'.
* Set SM System Id
PERFORM add_attribute_to_node USING header_node 'SM_SYSTEM_ID' 'sm_sysid'.
* Set SM system release
PERFORM add_attribute_to_node USING header_node 'SM_SYSTEM_REL' 'sm_saprl'.
* Set execution mode
IF r_back IS INITIAL.
lv_mode = 'FOREGROUND'.
ELSE.
lv_mode = 'BACKGROUND'.
ENDIF.
PERFORM add_attribute_to_node USING header_node 'MODE' lv_mode.

PERFORM add_attribute_to_node USING header_node 'PRD_LOGSYS' 'gv_logsys'.

CALL FUNCTION 'PARTNER_LOGICAL_SYSTEM_GET'
IMPORTING
p_logsys = lv_logsys
EXCEPTIONS
own_logical_system_not_defined = 1
OTHERS = 2.
IF sy-subrc <> 0.
lv_logsys = 'UNDEFINED'.
ENDIF.
PERFORM add_attribute_to_node USING header_node 'LOCAL_LOGSYS' lv_logsys.

* Add unicode length of the current system
PERFORM add_attribute_to_node USING header_node 'UCLEN' unicodelg.
* Add active industry solution when applicable
PERFORM add_attribute_to_node USING header_node 'ACTIVE_INDUSTRY_SOLUTION' 'active_industry_solution'.
* Add usage only
PERFORM add_attribute_to_node USING header_node 'USAGE_ONLY' 'usageonl'.
PERFORM add_attribute_to_node USING header_node 'IA_DATA_ONLY' 'ia_data_only'.
PERFORM add_attribute_to_node USING header_node 'REQUIRE_SOLMAN_RFC' 'require_solman_rfc'.
PERFORM add_attribute_to_node USING header_node 'TESTING_DATA_ONLY' 'testing_data_only'.
PERFORM add_attribute_to_node USING header_node 'NO_SUET_DATA' 'no_suet_data'.
PERFORM add_attribute_to_node USING header_node 'EXTRACT_DEBUG' 'extract_debug'.
PERFORM add_attribute_to_node USING header_node 'EXTRACT_FULL_USAGE' 'extract_full_usage'.
PERFORM add_attribute_to_node USING header_node 'EXTRACT_WORKFLOW_DATA' 'extract_workflow_data'.
PERFORM add_attribute_to_node USING header_node 'GET_AGR_USERS' 'get_agr_users'.

* Add version specific header attributes
* PERFORM version_specific_header_attrib USING header_node.
PERFORM add_attribute_to_node USING header_node 'MINI_VERSION' 'HEADER_MINI_VERSION'.
* Add kernel data
PERFORM get_kernel_data CHANGING k_release k_patch.
PERFORM add_attribute_to_node USING header_node 'KERNEL_RELEASE' k_release.
PERFORM add_attribute_to_node USING header_node 'KERNEL_PATCH' k_patch.
PERFORM add_attribute_to_node USING header_node 'EXTRACT_USER_DATA' 'extract_user_data'.
PERFORM add_attribute_to_node USING header_node 'SM_SYSTEM' 'p_sm_rfc'.
* Set SM System Id
PERFORM add_attribute_to_node USING header_node 'SM_SYSTEM_ID' 'sm_sysid'.
* Set SM system release
PERFORM add_attribute_to_node USING header_node 'SM_SYSTEM_REL' 'sm_saprl'.
* Set bw extraction system data
* BW system
PERFORM add_attribute_to_node USING header_node 'BW_DESTINATION' 'p_bw_rfc'.

* CALL FUNCTION 'SCT2_GET_T000_ENTRY_RFC' DESTINATION p_bw_rfc
* IMPORTING
* ev_sysid = lv_rfc_sysid
* ev_mandt = lv_rfc_mandt
* EXCEPTIONS
* system_failure = 1
* communication_failure = 2.

* Set BW System ID
PERFORM add_attribute_to_node USING header_node 'BW_SYSID' 'lv_rfc_sysid'.
* Set BW Client
PERFORM add_attribute_to_node USING header_node 'BW_CLIENT' 'lv_rfc_mandt'.
* Set the local system and target system's types
PERFORM add_system_types_to_header USING header_node.
PERFORM append_xml_element_to_file USING header_node 'X'.
ENDFORM. " add_document_header

*&---------------------------------------------------------------------*
*& Form ADD_SYSTEM_TYPES_TO_HEADER
*&---------------------------------------------------------------------*
* …….. *
*----------------------------------------------------------------------*
FORM add_system_types_to_header
USING iv_header_node TYPE REF TO if_ixml_element.

DATA: ls_local_system TYPE t000,
ls_target_system TYPE t000,
ls_rfcdes TYPE rfcdes,
ls_rfcdisplay TYPE rfcdisplay.

DATA: lv_prefix TYPE string,
lv_suffix TYPE string,
lv_target_mandt TYPE mandt.

DATA: options TYPE TABLE OF rfc_db_opt,
fields TYPE TABLE OF rfc_db_fld,
t000_itab TYPE TABLE OF t000.

* Local system's type
SELECT SINGLE *
FROM t000
INTO CORRESPONDING FIELDS OF ls_local_system
WHERE mandt EQ sy-mandt.

IF sy-subrc IS INITIAL.
PERFORM add_attribute_to_node USING iv_header_node
'LOCAL_SYSTEM_TYPE' ls_local_system-cccategory.
MESSAGE s000(conv) WITH 'LOCAL_SYSTEM_TYPE ='
ls_local_system-cccategory.
ENDIF.

* Target system's type
SELECT SINGLE *
FROM rfcdes BYPASSING BUFFER
INTO CORRESPONDING FIELDS OF ls_rfcdes
WHERE rfcdest EQ 'DR1'.

IF sy-subrc IS INITIAL.
SPLIT ls_rfcdes-rfcoptions AT ',M=' INTO lv_prefix lv_suffix.
IF sy-subrc IS INITIAL AND NOT lv_suffix IS INITIAL.
lv_target_mandt = lv_suffix(3).
ENDIF.
ENDIF.

* Get target system T000 table
CALL FUNCTION 'RFC_READ_TABLE' DESTINATION 'DR1'
EXPORTING
query_table = 'T000'
delimiter = space
no_data = space
rowskips = 0
rowcount = 0
TABLES
options = options
fields = fields
data = t000_itab
EXCEPTIONS
table_not_available = 1
table_without_data = 2
option_not_valid = 3
field_not_valid = 4
not_authorized = 5
data_buffer_exceeded = 6
OTHERS = 7.

IF sy-subrc IS INITIAL AND NOT t000_itab IS INITIAL.
IF NOT lv_target_mandt IS INITIAL.
* Read target T000 with mandt
READ TABLE t000_itab INTO ls_target_system
WITH KEY mandt = lv_target_mandt.
IF NOT sy-subrc IS INITIAL.
* Fallback: Mandt not found Read target T000 with cccategory = 'P'
* CONCATENATE
* 'No results in target system T000 table'
* 'for target MANDT found:' lv_target_mandt
* 'Reading T000 with cccategory = P'
* INTO gt_etl_message-message SEPARATED BY space.

* PERFORM add_message USING gt_etl_message-message abap_false.

    READ TABLE t000\_itab INTO ls\_target\_system  
      WITH KEY cccategory = 'P'.  
  ENDIF.  
ELSE.  

* Fallback: No mandt: Read target T000 with cccategory = 'P'
* CONCATENATE
* 'Failed to get MANDT, Reading target system T000'
* 'table with cccategory = P'
* INTO gt_etl_message-message SEPARATED BY space.

* PERFORM add_message USING gt_etl_message-message abap_false.

  READ TABLE t000\_itab INTO ls\_target\_system  
    WITH KEY cccategory = 'P'.  
ENDIF.  

ENDIF.

IF NOT ls_target_system IS INITIAL.
PERFORM add_attribute_to_node USING iv_header_node
'TARGET_SYSTEM_TYPE' ls_target_system-cccategory.
ENDIF.

ENDFORM. " ADD_SYSTEM_TYPES_TO_HEADER

*---------------------------------------------------------------------*
* FORM GET_KERNEL_DATA *
*---------------------------------------------------------------------*
* …….. *
*---------------------------------------------------------------------*
* --> EV_KRELEASE *
* --> EV_KLEVEL *
*---------------------------------------------------------------------*
FORM get_kernel_data CHANGING ev_krelease
ev_klevel.

TYPES: BEGIN OF t_kernel_version,
key(21) TYPE c,
data(69) TYPE c,
END OF t_kernel_version.

DATA: lt_kernel_version TYPE TABLE OF t_kernel_version
WITH HEADER LINE,
ls_kernel_version TYPE t_kernel_version.

CALL 'SAPCORE' ID 'ID' FIELD 'VERSION'
ID 'TABLE' FIELD lt_kernel_version-*sys*.

READ TABLE lt_kernel_version INTO ls_kernel_version INDEX 12.
ev_krelease = ls_kernel_version-data.
READ TABLE lt_kernel_version INTO ls_kernel_version INDEX 15.
ev_klevel = ls_kernel_version-data.

ENDFORM. "GET_KERNEL_DATA
*---------------------------------------------------------------------*
* FORM create_xml_node(创建XML 节点) *
*---------------------------------------------------------------------*
FORM create_xml_node USING
VALUE(pi_pdocument) TYPE REF TO if_ixml_document
VALUE(parent_element) TYPE REF TO if_ixml_element
VALUE(pi_xml_node) TYPE string
CHANGING VALUE(pc_elem) TYPE REF TO if_ixml_element.

pc_elem = pi_pdocument->create_simple_element(
name = pi_xml_node
parent = parent_element ).

ENDFORM. " create_xml_node

*---------------------------------------------------------------------*
* FORM create_xml_node_value_cdata *
*---------------------------------------------------------------------*
FORM create_xml_node_value_cdata USING
VALUE(pi_pdocument) TYPE REF TO if_ixml_document
VALUE(parent_element) TYPE REF TO if_ixml_element
VALUE(pi_xml_node) TYPE string
VALUE(pi_value) TYPE any
VALUE(pi_with_cdata) TYPE c
CHANGING
VALUE(pc_elem) TYPE REF TO if_ixml_element.

DATA: l_value TYPE string.

IF pi_with_cdata = 'X'.

DATA: rval TYPE REF TO if\_ixml\_cdata\_section.  
l\_value = pi\_value.  
CALL METHOD pi\_pdocument->create\_cdata\_section  
  EXPORTING  
    cdata = l\_value  
  RECEIVING  
    rval  = rval.

pc\_elem =  pi\_pdocument->create\_simple\_element(  
                                 name   = pi\_xml\_node  

* value = l_value
parent = parent_element ).

CALL METHOD pc\_elem->append\_child  
  EXPORTING  
    new\_child = rval.

ELSE.
l_value = pi_value.
pc_elem = pi_pdocument->create_simple_element(
name = pi_xml_node
value = l_value
parent = parent_element ).
ENDIF.

ENDFORM. " create_xml_node_value

*---------------------------------------------------------------------*
* FORM close_xml_file *
*---------------------------------------------------------------------*
FORM close_xml_file.

DATA: stream_size TYPE i,
msg_line1(70) TYPE c,
msg_line2(70) TYPE c,
msg_line3(70) TYPE c.

PERFORM append_string_to_file USING ''.

* flush the buffered data to the file - last time
stream_size = g_ostream->get_num_written_raw( ).
PERFORM append_itab_to_file USING stream_size g_buffer_itab.

* Write the file to original location
PERFORM move_file USING temp_fore_filename
temp_back_filename
p_fore
p_back
r_fore.

* Display finish message
IF panaya_test_mode IS INITIAL.
msg_line1 = 'The data extraction was finished successfully.'.
IF no_usage = 'X'.
msg_line2 = 'Please note: No usage statistics were found.'.
msg_line3 = 'Contact Panaya support for assistance.'.
IF popup_ok = 'X'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT_LO'
EXPORTING
titel = 'Panaya'
textline1 = msg_line1
textline2 = msg_line2
textline3 = msg_line3
start_column = 15
start_row = 6.
ELSE.
WRITE:/ msg_line1.
WRITE:/ msg_line2.
WRITE:/ msg_line3.
ENDIF.
ELSE.
IF popup_ok = 'X'.

    CALL FUNCTION 'POPUP\_TO\_DISPLAY\_TEXT\_LO'  
      EXPORTING  
        titel        = 'Panaya'  
        textline1    = msg\_line1  
        start\_column = 15  
        start\_row    = 6.  
  ELSE.  
    WRITE:/ msg\_line1.  
    WRITE:/ msg\_line2.  
    WRITE:/ msg\_line3.  
  ENDIF.

ENDIF.  

ENDIF.

ENDFORM. "close_xml_file

*---------------------------------------------------------------------*
* FORM append_string_to_file *
*---------------------------------------------------------------------*
FORM append_string_to_file USING str TYPE string.

CALL METHOD g_ostream->write_string
EXPORTING
string = new_line.
CALL METHOD g_ostream->write_string
EXPORTING
string = str.
CALL METHOD g_ostream->write_string
EXPORTING
string = new_line.

ENDFORM. "append_string_to_file

*---------------------------------------------------------------------*
* FORM append_itab_to_file *
*---------------------------------------------------------------------*
FORM append_itab_to_file USING i_stream_size TYPE i
i_tab_converted_data TYPE xml_tab_type.

DATA: l_oref TYPE REF TO cx_root,
text TYPE string.
DATA: l_file TYPE string.
DATA: ls_file TYPE xml_line_type.
DATA: l_remaining_size TYPE i.
DATA: l_recsize TYPE i.

IF NOT r_fore IS INITIAL.
l_file = temp_fore_filename.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = i_stream_size
filename = l_file
filetype = 'BIN'
append = g_file_append_mode
CHANGING
data_tab = i_tab_converted_data
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
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.

IF sy-subrc <> 0.  
  DATA: explan TYPE string.  
  IF sy-subrc = 17.  
    explan = 'Disk is full'.  
  ELSE.  
    explan = sy-subrc.  
  ENDIF.  
  MESSAGE e000(conv) WITH 'Problem writing to file: ' explan.  
ENDIF.  

ENDIF.

* Server file
IF NOT r_back IS INITIAL.
* Open file
IF g_file_append_mode IS INITIAL.
OPEN DATASET temp_back_filename FOR OUTPUT IN BINARY MODE.
ELSE.
OPEN DATASET temp_back_filename FOR APPENDING IN BINARY MODE.
ENDIF.
* Write file
l_remaining_size = i_stream_size.
LOOP AT i_tab_converted_data INTO ls_file.
IF l_remaining_size < 256.
l_recsize = l_remaining_size.
ELSE.
l_recsize = 256.
ENDIF.

  TRANSFER ls\_file TO temp\_back\_filename  LENGTH l\_recsize.  
  SUBTRACT l\_recsize FROM l\_remaining\_size.  
ENDLOOP.  

* Close file
CLOSE DATASET temp_back_filename .
IF sy-subrc IS INITIAL.
* MESSAGE s171(26) WITH p_back.
* File & created
ELSE.
* MESSAGE e005(ps).
* Cannot close output file
ENDIF.
ENDIF.

* The first time we write the file in overwrite mode, then we switch
* to append mode
IF g_file_append_mode IS INITIAL.
g_file_append_mode = 'X'.
ENDIF.

ENDFORM. "append_itab_to_file

*&---------------------------------------------------------------------*
*& Form MOVE_FILE
*&---------------------------------------------------------------------*
* Move file from initial location to the location specified by
* user
*----------------------------------------------------------------------*
FORM move_file USING p_temp_fore_file
p_temp_back_file
p_orig_fore_file
p_orig_back_file
p_is_fore.

DATA: explan TYPE string,
error_msg TYPE string,
curr_line_len TYPE i,
data(256) TYPE c,
filename1 TYPE string,
filename2 TYPE string.
DATA: l_oref TYPE REF TO cx_root,
text TYPE string,
text_len TYPE i,
param1(50) TYPE c,
param2(50) TYPE c,
param3(50) TYPE c,
param4(50) TYPE c.

IF NOT p_is_fore IS INITIAL.

* Copy the file to original location
filename1 = p_temp_fore_file.
filename2 = p_orig_fore_file.
CALL METHOD cl_gui_frontend_services=>file_copy
EXPORTING
source = filename1
destination = filename2
overwrite = 'X'
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
disk_full = 4
access_denied = 5
file_not_found = 6
destination_exists = 7
unknown_error = 8
path_not_found = 9
disk_write_protect = 10
drive_not_ready = 11
OTHERS = 12.

IF sy-subrc <> 0.  
  IF sy-subrc = 4.  
    explan = 'Disk is full'.  
  ELSE.  
    explan = sy-subrc.  
  ENDIF.  
  MESSAGE e001(00) WITH 'Problem writing to file: '  
  p\_orig\_fore\_file  
   explan.  
ENDIF.

* Delete the temporary file
* DATA: rc TYPE i.
* CALL METHOD cl_gui_frontend_services=>file_delete
* EXPORTING
* filename = p_temp_fore_file
* CHANGING
* rc = rc
* EXCEPTIONS
* file_delete_failed = 1
* cntl_error = 2
* error_no_gui = 3
* file_not_found = 4
* access_denied = 5
* unknown_error = 6
* OTHERS = 7.
* IF sy-subrc <> 0.
* explan = sy-subrc.
* MESSAGE e001(00) WITH 'Problem deleting file: ' p_temp_fore_file
* ' return code (RC) = ' rc.
* ENDIF.

ELSE.
* Open the temp file
OPEN DATASET p_temp_back_file FOR INPUT IN BINARY
MODE MESSAGE error_msg.

OPEN DATASET p\_orig\_back\_file FOR OUTPUT IN BINARY MODE  
   MESSAGE error\_msg.

* Transfer the data to destination file
DO.
READ DATASET p_temp_back_file INTO data LENGTH curr_line_len.
IF sy-subrc <> 0.
TRANSFER data TO p_orig_back_file
LENGTH curr_line_len.
EXIT.
ENDIF.
TRANSFER data TO p_orig_back_file LENGTH curr_line_len.
ENDDO.

* Close both files
CLOSE DATASET p_temp_back_file.
CLOSE DATASET p_orig_back_file.

* Delete the file from current directory
DELETE DATASET p_temp_back_file.

IF sy-subrc <> 0.  
  MESSAGE e001(00) WITH 'Problem deleting file: ' p\_temp\_back\_file  
    ' SUBC: ' sy-subrc.  
  EXIT.  
ENDIF.  

ENDIF.
ENDFORM. " MOVE_FILE

*---------------------------------------------------------------------*
* FORM append_xml_element_to_file *
*---------------------------------------------------------------------*
FORM append_xml_element_to_file USING i_element TYPE REF TO if_ixml_element
i_remove_node TYPE c.

DATA: stream_size TYPE i.

stream_size = g_ostream->get_num_written_raw( ).

IF ( stream_size > buffer_max_size ).
PERFORM append_itab_to_file USING stream_size g_buffer_itab.
g_ostream = g_iref_pstreamfactory->create_ostream_itable(
table = g_buffer_itab ).
CALL METHOD g_ostream->set_pretty_print
EXPORTING
pretty_print = 'X'.
CLEAR g_buffer_itab.
ENDIF.

CALL METHOD g_ostream->write_string
EXPORTING
string = new_line.
CALL METHOD i_element->render( ostream = g_ostream ).

IF i_remove_node = 'X'.
CALL METHOD i_element->remove_node.
CLEAR i_element.
ENDIF.

ENDFORM. "append_xml_element_to_file

*---------------------------------------------------------------------*
* FORM open_xml_file *
*---------------------------------------------------------------------*
FORM open_xml_file.

DATA: header_string TYPE string.

* Initialize stream factory
g_iref_pstreamfactory = g_iref_pixml->create_stream_factory( ).
g_ostream = g_iref_pstreamfactory->create_ostream_itable(
table = g_buffer_itab ).
CALL METHOD g_ostream->set_pretty_print
EXPORTING
pretty_print = 'X'.

CONCATENATE ''
INTO header_string.

CALL METHOD g_ostream->write_string
EXPORTING
string = header_string.
PERFORM append_string_to_file USING ''.

ENDFORM. "open_xml_file

*&---------------------------------------------------------------------*
*& Form add_used_memory_to_log
*&---------------------------------------------------------------------*
FORM add_used_memory_to_log.

DATA: mem_size TYPE abap_msize,
mem(25) TYPE c,
mem_text(50) TYPE c.

IF sy-batch = 'X'.
* Used memory log message
CALL METHOD cl_abap_memory_utilities=>get_total_used_size
IMPORTING
size = mem_size.
mem = mem_size.
CONCATENATE 'Used memory:' mem INTO mem_text SEPARATED BY space.
* MESSAGE s000(conv) WITH mem_text.
* PERFORM add_message USING mem_text abap_true.
ENDIF.

ENDFORM. " add_used_memory_to_log

*---------------------------------------------------------------------*
* FORM update_progress *
*---------------------------------------------------------------------*
FORM update_progress USING text.

DATA: batch_text(50) TYPE c,
curr_step(2) TYPE c,
total_steps(2) TYPE c,
steps_text(50) TYPE c,
progress_text(512) TYPE c.

curr_object_type = curr_object_type + 1.
percentage = percentage + obj_perc.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = percentage
text = text.

IF sy-batch = 'X'.
curr_step = curr_object_type.
total_steps = num_object_types.
CONCATENATE '(' curr_step '/' total_steps ')' INTO steps_text.
CONCATENATE text steps_text INTO progress_text SEPARATED BY space.
* PERFORM ADD_MESSAGE USING PROGRESS_TEXT ABAP_TRUE.

PERFORM add\_used\_memory\_to\_log.  

ENDIF.

ENDFORM. "update_progress