REPORT ZXML_FILE .

type-pools: ixml.
tables: vbak.

types: begin of ty_vbak,
       vbeln type vbak-vbeln,
       auart type vbak-auart,
      end of ty_vbak.

data: ty_ftp type standard table of zftpinfo,
      wa_ftp type zftpi,
      l_slen type i,
      lv_user(30) type c,
      lv_pwd(30) type c,
      c_key type i value 26101957.

data: begin of t_title occurs 0,
      title(4000) type c,
      end of t_title.
data: temp_file(200) type c.
data: to_file type char30000.
data: c_gw type string.
data: mi_handle type i.

data: t_vbak type ty_vbak occurs 0 with header line.

selection-screen begin of block b1 with frame title text-001.
select-options: so_num for vbak-vbeln.
selection-screen end of block b1.

*START OF SELECTION
start-of-selection.

select
  vbeln auart
  from vbak into corresponding fields of table t_vbak
  where vbeln in so_num.

"" FOR GETTING FTP SEVER CONNECTION INFO FROM MAINTAING TABLE
select * from zftpinfo client specified
    into corresponding fields of table ty_ftp
    where mandt = sy-mandt
    and   zcountry = 'BD'.

  clear wa_ftp.
  read table ty_ftp into wa_ftp index 1.

*----CONNECTING TO FTP SERVER.
  perform ftp_connect using wa_ftp-zuser wa_ftp-zpasswd wa_ftp-zhost.

*----CHECKING SUCCESSFUL FTP CONNECTION
  check sy-subrc = 0.

    if t_vbak[] is not initial.
    perform c_xml. " FOR CREATING XML FILE
    endif.

form c_xml.

  clear t_title[].
 "" SERVER DESTINATION FILE PATH EX: /.../.../.../
  concatenate 'SERVER FILE PATH' 'SOINFO' '.xml'  into to_file.

*----BUILDING THE XML FILE
  t_title-title = '<?xml version="1.0" encoding="UTF-8"?>'.
  append t_title.
  t_title-title = '<n1:SalesOderInfo xmlns:n1="SalesOderInfo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.
  append t_title.

  loop at t_vbak.
    concatenate '<SONUM>' t_vbak-vbeln '</SONUM>' into temp_file.
    t_title-title = temp_file.
    append t_title.
    clear temp_file.

    concatenate '<DOCTYPE>' t_vbak-auart '</DOCTYPE>' into temp_file.
    t_title-title = temp_file.
    append t_title.
    clear temp_file.

    endloop.

    t_title-title = '</n1:SalesOderInfo>'.
    append t_title.

  perform r3_ftp using to_file.

endform.



form r3_ftp using to_file.
  call function 'FTP_R3_TO_SERVER'
    exporting
      handle         = mi_handle
      fname          = to_file          "file path of destination system
      character_mode = 'X'
    tables
      text           = t_title
    exceptions
      tcpip_error    = 1
      command_error  = 2
      data_error     = 3
      others         = 4.
endform.

form ftp_connect using lv_user lv_pwd lv_host.

  l_slen = strlen( lv_pwd ).

  call function 'HTTP_SCRAMBLE'
    exporting
      source      = lv_pwd
      sourcelen   = l_slen
      key         = c_key
    importing
      destination = lv_pwd.

  call function 'FTP_CONNECT'
    exporting
      user            = lv_user "Your SAP-UNIX FTP user name (case sensitive)
      password        = lv_pwd  "Your SAP-UNIX server host name (case sensitive)
      host            = lv_host
      rfc_destination = 'SAPFTPA'
    importing
      handle          = mi_handle
    exceptions
      not_connected   = 1
      others          = 2.

endform.



Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com