*&---------------------------------------------------------------------* *& Report /EUTIN/DEMO_XML_UPLOAD *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT /eutin/demo_xml_upload. PARAMETERS: alv AS CHECKBOX DEFAULT 'X', database AS CHECKBOX. START-OF-SELECTION. PERFORM main. *&---------------------------------------------------------------------* *& Form MAIN *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* FORM main. DATA: lt_customers TYPE /eutin/t_customer, lv_customers_xml TYPE string. CLEAR: lt_customers[], lv_customers_xml. PERFORM upload_xml CHANGING lv_customers_xml. IF lv_customers_xml IS NOT INITIAL. PERFORM parse_xml USING lv_customers_xml CHANGING lt_customers[]. ENDIF. FREE lv_customers_xml. IF lt_customers[] IS NOT INITIAL. IF alv IS NOT INITIAL. PERFORM display_alv USING 'Content of uploaded file' lt_customers[]. ENDIF. IF database IS NOT INITIAL. CALL FUNCTION 'ENQUEUE_E_TABLE' EXPORTING mode_rstable = 'E' tabname = '/EUTIN/CUSTOMER' * VARKEY = * X_TABNAME = ' ' * X_VARKEY = ' ' * _SCOPE = '2' * _WAIT = ' ' * _COLLECT = ' ' EXCEPTIONS foreign_lock = 1 system_failure = 2 OTHERS = 3. IF sy-subrc NE 0. IF sy-msgty IS NOT INITIAL. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE sy-msgty. ENDIF. MESSAGE 'Enqueue failed' TYPE 'S' DISPLAY LIKE 'E'. RETURN. ENDIF. PERFORM insert_into_database USING lt_customers[]. CALL FUNCTION 'DEQUEUE_E_TABLE' EXPORTING mode_rstable = 'E' tabname = '/EUTIN/CUSTOMER' * VARKEY = * X_TABNAME = ' ' * X_VARKEY = ' ' * _SCOPE = '3' * _SYNCHRON = ' ' * _COLLECT = ' ' . ENDIF. ENDIF. ENDFORM. FORM upload_xml CHANGING ev_customers_xml TYPE string. DATA: lt_xml_content_table TYPE STANDARD TABLE OF string WITH DEFAULT KEY, lt_files TYPE filetable, ls_xml_content_line TYPE string, lv_filename TYPE string, lv_subrc TYPE i. FIELD-SYMBOLS: LIKE LINE OF lt_files. CLEAR: ev_customers_xml, lt_xml_content_table[], lt_files[], lv_filename. lv_filename = 'C:\customers.xml'. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = 'Upload Customer XML File' default_extension = 'xml' default_filename = lv_filename file_filter = 'XML File|*.xml|*.xml' * with_encoding = * initial_directory = * multiselection = CHANGING file_table = lt_files[] rc = lv_subrc * user_action = * file_encoding = EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy-subrc NE 0 OR lt_files[] IS INITIAL OR lv_subrc NE 1. RETURN. ENDIF. READ TABLE lt_files ASSIGNING INDEX 1. IF sy-subrc EQ 0. lv_filename = -filename. UNASSIGN . FREE lt_files. ENDIF. IF lv_filename IS INITIAL. RETURN. ENDIF. CALL METHOD cl_gui_frontend_services=>gui_upload EXPORTING filename = lv_filename filetype = 'ASC' * has_field_separator = SPACE * header_length = 0 * read_by_line = 'X' * dat_mode = SPACE * codepage = SPACE * ignore_cerr = ABAP_TRUE * replacement = '#' * virus_scan_profile = * IMPORTING * filelength = * header = CHANGING data_tab = lt_xml_content_table[] * isscanperformed = SPACE EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 not_supported_by_gui = 17 error_no_gui = 18 OTHERS = 19. IF sy-subrc NE 0. MESSAGE 'GUI_UPLOAD returned with exception code' TYPE 'S' DISPLAY LIKE 'E'. RETURN. ENDIF. LOOP AT lt_xml_content_table INTO ls_xml_content_line. CONCATENATE ev_customers_xml ls_xml_content_line INTO ev_customers_xml. ENDLOOP. FREE: lt_xml_content_table, ls_xml_content_line. ENDFORM. FORM parse_xml USING iv_customers_xml TYPE string CHANGING et_customers TYPE /eutin/t_customer. DATA: lr_cx_root TYPE REF TO cx_root, lv_message TYPE string. TRY. CALL TRANSFORMATION /eutin/customer SOURCE XML iv_customers_xml RESULT customers = et_customers[]. CATCH cx_root INTO lr_cx_root. lv_message = lr_cx_root->if_message~get_text( ). IF lv_message IS NOT INITIAL. MESSAGE lv_message TYPE 'I' DISPLAY LIKE 'E'. ENDIF. MESSAGE 'XML transformation failed' TYPE 'I' DISPLAY LIKE 'E'. CLEAR et_customers[]. RETURN. ENDTRY. ENDFORM. FORM display_alv USING iv_alv_title TYPE lvc_title it_any_table TYPE STANDARD TABLE. DATA: lr_alv_table TYPE REF TO cl_salv_table, lr_functions_list TYPE REF TO cl_salv_functions_list, lr_columns_table TYPE REF TO cl_salv_columns_table, lr_column_table TYPE REF TO cl_salv_column_table, lr_display_settings TYPE REF TO cl_salv_display_settings, lr_header_info TYPE REF TO cl_salv_form_header_info, lx_salv_msg TYPE REF TO cx_salv_msg, lx_salv_not_found TYPE REF TO cx_salv_not_found, lx_salv_data_error TYPE REF TO cx_salv_data_error, lx_salv_existing TYPE REF TO cx_salv_existing, lx_root TYPE REF TO cx_root, lv_error TYPE string, lv_counts TYPE i, lv_header_count TYPE c LENGTH 20, lv_header_text TYPE string. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IF it_any_table[] IS INITIAL. MESSAGE 'Output table is empty' TYPE 'I' DISPLAY LIKE 'W'. RETURN. ENDIF. TRY. *--- create ALV table cl_salv_table=>factory( IMPORTING r_salv_table = lr_alv_table CHANGING t_table = it_any_table[] ). *--- activate all standard functions lr_functions_list = lr_alv_table->get_functions( ). lr_functions_list->set_all( if_salv_c_bool_sap=>true ). *--- set optimized column size lr_columns_table = lr_alv_table->get_columns( ). lr_columns_table->set_optimize( if_salv_c_bool_sap=>true ). *--- enable output othe field MANDT lr_column_table ?= lr_columns_table->get_column( 'MANDT' ). lr_column_table->set_technical( if_salv_c_bool_sap=>false ). *--- set zebra layout lr_display_settings = lr_alv_table->get_display_settings( ). lr_display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ). *--- set title lr_display_settings->set_list_header( iv_alv_title ). *--- set header and display number of displayed entries DESCRIBE TABLE it_any_table LINES lv_counts. WRITE lv_counts TO lv_header_count LEFT-JUSTIFIED. CONCATENATE 'Number of entries:' lv_header_count INTO lv_header_text SEPARATED BY space. CREATE OBJECT lr_header_info EXPORTING text = lv_header_text. lr_alv_table->set_top_of_list( EXPORTING value = lr_header_info ). *--- display table lr_alv_table->display( ). *--- error handling CATCH cx_salv_msg INTO lx_salv_msg. CLEAR lv_error. lv_error = lx_salv_msg->if_message~get_text( ). MESSAGE lv_error TYPE 'E'. CATCH cx_salv_not_found INTO lx_salv_not_found. CLEAR lv_error. lv_error = lx_salv_not_found->if_message~get_text( ). MESSAGE lv_error TYPE 'E'. CATCH cx_salv_data_error INTO lx_salv_data_error. CLEAR lv_error. lv_error = lx_salv_data_error->if_message~get_text( ). MESSAGE lv_error TYPE 'E'. CATCH cx_salv_existing INTO lx_salv_existing. CLEAR lv_error. lv_error = lx_salv_existing->if_message~get_text( ). MESSAGE lv_error TYPE 'E'. CATCH cx_root INTO lx_root. "pragma or pseudo comment CATCH_ALL: okay, one to catch them all! *--- error tolerant mode: do not cause catchable runtime errors! CLEAR lv_error. lv_error = lx_root->if_message~get_text( ). MESSAGE lv_error TYPE 'E'. ENDTRY. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ENDFORM. FORM insert_into_database USING it_customers TYPE /eutin/t_customer. COMMIT WORK AND WAIT. INSERT /eutin/customer FROM TABLE it_customers. IF sy-subrc NE 0. ROLLBACK WORK. MESSAGE 'INSERT failed' TYPE 'S' DISPLAY LIKE 'E'. RETURN. ENDIF. COMMIT WORK AND WAIT. MESSAGE 'Done' TYPE 'S'. ENDFORM. *--- The End!