Difference between revisions of "SAP ABAP XSTRING"

From SapWiki
Line 39: Line 39:
 
DATA: l_lines        TYPE i.
 
DATA: l_lines        TYPE i.
  
filename = p_file.
+
l_filename = p_file.
  
 
* upload the file
 
* upload the file
   CALL FUNCTION 'C13Z_UPLOAD'
+
   CALL METHOD cl_gui_frontend_services=>gui_upload
 
     EXPORTING
 
     EXPORTING
*    CODEPAGE                = ' '
 
 
       filename                = l_filename
 
       filename                = l_filename
 
       filetype                = 'BIN'
 
       filetype                = 'BIN'
 +
*      header_length          = l_header_length
 
     IMPORTING
 
     IMPORTING
 
       filelength              = l_filelength
 
       filelength              = l_filelength
     TABLES
+
     CHANGING
       data_tab                = l_data_tab
+
       data_tab                = l_data_tab[]
 
     EXCEPTIONS
 
     EXCEPTIONS
       conversion_error        = 1
+
       file_open_error        = 1
       file_open_error         = 2
+
       file_read_error         = 2
       file_read_error        = 3
+
       no_batch                = 3
       invalid_type            = 4
+
       gui_refuse_filetransfer = 4
       no_batch                = 5
+
       invalid_type            = 5
       unknown_error          = 6
+
       no_authority            = 6
       invalid_table_width    = 7
+
       unknown_error          = 7
      gui_refuse_filetransfer = 8
+
       bad_data_format        = 8
      customer_error          = 9
+
       header_not_allowed      = 9
      no_authority            = 10
+
       separator_not_allowed  = 10
       bad_data_format        = 11
+
       header_too_long        = 11
       header_not_allowed      = 12
+
       unknown_dp_error        = 12
       separator_not_allowed  = 13
+
       access_denied          = 13
       header_too_long        = 14
+
       dp_out_of_memory        = 14
       unknown_dp_error        = 15
+
       disk_full              = 15
       access_denied          = 16
+
       dp_timeout              = 16
       dp_out_of_memory        = 17
+
       not_supported_by_gui    = 17
       disk_full              = 18
+
       error_no_gui            = 18
       dp_timeout              = 19
+
       OTHERS                  = 19.
       not_supported_by_gui    = 20
 
       error_no_gui            = 21
 
       OTHERS                  = 22.
 
 
 
 
   IF sy-subrc <> 0 .
 
   IF sy-subrc <> 0 .
 
   ENDIF.
 
   ENDIF.
Line 97: Line 93:
 
   cl_abap_conv_in_ce=>create( input = l_xstring )->read( IMPORTING data = l_string ).
 
   cl_abap_conv_in_ce=>create( input = l_xstring )->read( IMPORTING data = l_string ).
  
 +
* transformar string a tabla interna
 
   WHILE l_string IS NOT INITIAL.
 
   WHILE l_string IS NOT INITIAL.
 
     SPLIT l_string AT cl_abap_char_utilities=>cr_lf INTO l_string2 l_string.
 
     SPLIT l_string AT cl_abap_char_utilities=>cr_lf INTO l_string2 l_string.

Revision as of 18:52, 24 February 2021

Leer atchivo de texto como binario

Ejemplo 1

DATA: lo_c2x  TYPE REF TO cl_abap_conv_out_ce.

DATA l_string TYPE string.
DATA l_string2 TYPE string.
DATA l_xstring TYPE xstring.
DATA l_filename TYPE string.

CALL METHOD cl_faa_tenv_services=>gui_upload_xstring
  EXPORTING
    id_fullpath = l_filename
  IMPORTING
    ed_xstring  = l_xstring.

" convert xstring to string for output
cl_abap_conv_in_ce=>create( input = l_xstring )->read( IMPORTING data = l_string ).

WHILE l_string IS NOT INITIAL.
  SPLIT l_string AT cl_abap_char_utilities=>cr_lf INTO l_string2 l_string.
  IF NOT l_string2 CO cl_abap_char_utilities=>cr_lf.
    MOVE l_string2 TO it_entrada.
    APPEND it_entrada.
  ENDIF.

ENDWHILE.

Ejemplo 2

DATA l_string TYPE string.
DATA l_string2 TYPE string.
DATA l_xstring TYPE xstring.
DATA l_filename TYPE string.

DATA: l_filelength    TYPE i.
DATA: l_data_tab      LIKE rcgrepfile OCCURS 10 WITH HEADER LINE.
DATA: l_auth_filename LIKE authb-filename.
DATA: l_lines         TYPE i.

l_filename = p_file.

* upload the file
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = l_filename
      filetype                = 'BIN'
*      header_length           = l_header_length
    IMPORTING
      filelength              = l_filelength
    CHANGING
      data_tab                = l_data_tab[]
    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 <> 0 .
  ENDIF.

  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = l_filelength
*     FIRST_LINE   = 0
*     LAST_LINE    = 0
    IMPORTING
      buffer       = l_xstring
    TABLES
      binary_tab   = l_data_tab
    EXCEPTIONS
      failed       = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

*" convert xstring to string for output
  cl_abap_conv_in_ce=>create( input = l_xstring )->read( IMPORTING data = l_string ).

* transformar string a tabla interna
  WHILE l_string IS NOT INITIAL.
    SPLIT l_string AT cl_abap_char_utilities=>cr_lf INTO l_string2 l_string.
    IF NOT l_string2 CO cl_abap_char_utilities=>cr_lf.
      MOVE l_string2 TO it_entrada.
      APPEND it_entrada.
    ENDIF.
  ENDWHILE.

string to xstring => xstring to string

  • ejemplo 1
  lo_c2x = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
  lo_c2x->convert( EXPORTING data   = l_string
                   IMPORTING buffer = l_xstring ).

" convert xstring to string for output
cl_abap_conv_in_ce=>create( input = l_xstring )->read( IMPORTING data = l_string ).

  • ejemplo 2
l_xstring = cl_abap_codepage=>convert_to( source = l_string codepage = 'UTF-8' ).

  CALL METHOD cl_http_utility=>if_http_utility~decode_utf8
    EXPORTING
      encoded   = l_xstring
    RECEIVING
      unencoded = l_string.

Leer archivo de texto como UTF-8

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = l_filename
      filetype                = 'ASC'
      CODEPAGE                = '4110'
    TABLES
      data_tab                = it_entrada
    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
      OTHERS                  = 17.
  if sy-subrc <> 0 .
    endif.

Grabar XSTRING en equipo como binario

Leer archivo XML

desde un string en BASE64 obtener PDF binario

Leer Arhivo PDF y Visualizarlo