Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 4.31 KB | None | 0 0
  1. method save_xml_file.
  2.  
  3.   data: lt_itab_xml type standard table of char2048,
  4.         lv_path type string,
  5.         lv_filename type string,
  6.         lv_path_2 type string,
  7.         lv_file_encoding type string.
  8.  
  9.  
  10.   lt_itab_xml = it_xml.
  11.   lv_path     = iv_path.
  12.  
  13.   "Вызываем диалог сохранения, если не указали путь вручную
  14.   if lv_path is initial.
  15.     cl_gui_frontend_services=>file_save_dialog(
  16.       exporting
  17.         window_title         = 'Сохранение XML файла...'
  18.         default_file_name    = 'Заказ поставщику'
  19.         file_filter          = 'XML файл (*.xml)|*.xml'
  20.       changing
  21.         filename             = lv_filename
  22.         path                 = lv_path_2
  23.         fullpath             = lv_path
  24.       exceptions
  25.         cntl_error           = 1
  26.         error_no_gui         = 2
  27.         not_supported_by_gui = 3
  28.         others               = 4 ).
  29.  
  30.     if sy-subrc <> 0.
  31.       message id sy-msgid type sy-msgty number sy-msgno
  32.                  with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  33.     endif.
  34.   endif.
  35.  
  36.   " <<< Изменяем название кодировки в файле XML  на "UTF-8" и получаем код этой кодировки
  37.   "**************************************************************************************
  38.   data: lv_sap_codepage type cpcodepage,
  39.         lv_codepage type  abap_encoding.
  40.  
  41.   field-symbols: <ls_itab_xml>     like line of lt_itab_xml.
  42.  
  43.   "Это нужно что бы в файле XML стоял формат 'UTF-8', а не 'utf-16'.Хотя по факту он и так будет 'UTF-8'(ниже указываем его) почему так надо - ХЗ
  44.   loop at lt_itab_xml assigning <ls_itab_xml>.
  45.     replace first occurrence of substring 'utf-16' in <ls_itab_xml> with 'UTF-8' ignoring case.
  46.   endloop.
  47.  
  48.   "Получим код кодировки 'UTF-8'
  49.   call function 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  50.     exporting
  51.       external_name = 'UTF-8'
  52.     importing
  53.       sap_codepage  = lv_sap_codepage
  54.     exceptions
  55.       not_found     = 1
  56.       others        = 2.
  57.   if sy-subrc <> 0.
  58.     message id sy-msgid type sy-msgty number sy-msgno
  59.             with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  60.   endif.
  61.  
  62.   lv_codepage = lv_sap_codepage.
  63.   "**************************************************************************************
  64.   " >>> Изменяем название кодировки в файле XML  на "UTF-8" и получаем код этой кодировки
  65.  
  66.  
  67.   "Сохраняем файл
  68.   if lv_path is not initial.
  69.     call method cl_gui_frontend_services=>gui_download
  70.       exporting
  71.         filename                 = lv_path
  72.         codepage                 = lv_codepage  " или подставить код '4110' - это код для кодировки 'UTF-8'
  73.         write_lf                 = abap_false   " Не добавлять переводы строк после строки таблицы
  74.         trunc_trailing_blanks    = abap_true    " Не добавлять пробелы после строк (для CHAR полей)
  75.       changing
  76.         data_tab                 = lt_itab_xml
  77.       exceptions
  78.         file_write_error         = 1
  79.         no_batch                 = 2
  80.         gui_refuse_filetransfer  = 3
  81.         invalid_type             = 4
  82.         no_authority             = 5
  83.         unknown_error            = 6
  84.         header_not_allowed       = 7
  85.         separator_not_allowed    = 8
  86.         filesize_not_allowed     = 9
  87.         header_too_long          = 10
  88.         dp_error_create          = 11
  89.         dp_error_send            = 12
  90.         dp_error_write           = 13
  91.         unknown_dp_error         = 14
  92.         access_denied            = 15
  93.         dp_out_of_memory         = 16
  94.         disk_full                = 17
  95.         dp_timeout               = 18
  96.         file_not_found           = 19
  97.         dataprovider_exception   = 20
  98.         control_flush_error      = 21
  99.         not_supported_by_gui     = 22
  100.         error_no_gui             = 23
  101.         others                   = 24.
  102.  
  103.     if sy-subrc <> 0.
  104.       message id sy-msgid type sy-msgty number sy-msgno
  105.       with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  106.     endif.
  107.   endif.
  108. endmethod.                    "convert_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement