Difference between revisions of "SAP ABAP XSTRING"

From SapWiki
Line 1: Line 1:
 
==Leer atchivo de texto como binario==  
 
==Leer atchivo de texto como binario==  
 +
===Ejemplo 1===
 
  <nowiki>
 
  <nowiki>
 
DATA: lo_c2x  TYPE REF TO cl_abap_conv_out_ce.
 
DATA: lo_c2x  TYPE REF TO cl_abap_conv_out_ce.
Line 25: Line 26:
  
 
ENDWHILE.
 
ENDWHILE.
 +
</nowiki>
 +
===Ejemplo 2===
 +
<nowiki>
 +
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.
 +
 +
filename = p_file.
 +
 +
* upload the file
 +
  CALL FUNCTION 'C13Z_UPLOAD'
 +
    EXPORTING
 +
*    CODEPAGE                = ' '
 +
      filename                = l_filename
 +
      filetype                = 'BIN'
 +
    IMPORTING
 +
      filelength              = l_filelength
 +
    TABLES
 +
      data_tab                = l_data_tab
 +
    EXCEPTIONS
 +
      conversion_error        = 1
 +
      file_open_error        = 2
 +
      file_read_error        = 3
 +
      invalid_type            = 4
 +
      no_batch                = 5
 +
      unknown_error          = 6
 +
      invalid_table_width    = 7
 +
      gui_refuse_filetransfer = 8
 +
      customer_error          = 9
 +
      no_authority            = 10
 +
      bad_data_format        = 11
 +
      header_not_allowed      = 12
 +
      separator_not_allowed  = 13
 +
      header_too_long        = 14
 +
      unknown_dp_error        = 15
 +
      access_denied          = 16
 +
      dp_out_of_memory        = 17
 +
      disk_full              = 18
 +
      dp_timeout              = 19
 +
      not_supported_by_gui    = 20
 +
      error_no_gui            = 21
 +
      OTHERS                  = 22.
 +
 +
  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 ).
 +
 +
  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.
 
</nowiki>
 
</nowiki>
  

Revision as of 01:03, 18 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.

filename = p_file.

* upload the file
  CALL FUNCTION 'C13Z_UPLOAD'
    EXPORTING
*     CODEPAGE                = ' '
      filename                = l_filename
      filetype                = 'BIN'
    IMPORTING
      filelength              = l_filelength
    TABLES
      data_tab                = l_data_tab
    EXCEPTIONS
      conversion_error        = 1
      file_open_error         = 2
      file_read_error         = 3
      invalid_type            = 4
      no_batch                = 5
      unknown_error           = 6
      invalid_table_width     = 7
      gui_refuse_filetransfer = 8
      customer_error          = 9
      no_authority            = 10
      bad_data_format         = 11
      header_not_allowed      = 12
      separator_not_allowed   = 13
      header_too_long         = 14
      unknown_dp_error        = 15
      access_denied           = 16
      dp_out_of_memory        = 17
      disk_full               = 18
      dp_timeout              = 19
      not_supported_by_gui    = 20
      error_no_gui            = 21
      OTHERS                  = 22.

  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 ).

  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