ABAP SAP ~ AbapFox DATA_TO_DO and DOM_TO_DATA. Transform Internal table into XML files and vice versa.




If you ever had to move data between environments or use the test environment data quality always found trouble doing this. So here follows a program to download and upload all tables per package.
For that i transform the internal table in XML files and then, the xml file back in the internal table.
The magic is in this two functions:

DATA_TO_DO and DOM_TO_DATA

Farewell!


/FOX/BACKUP_TIME_II

*&---------------------------------------------------------------------*
*& Report  /GEE/BACKUP_TIME
*& From AbapFox - www.Abapfox.org
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  /fox/backup_time_ii.

tables: tadir.

select-options: s_class for tadir-devclass.

parameters p_path type string obligatory.

parameters: c_do radiobutton group g1,
            c_up radiobutton group g1.
parameters: p_local as checkbox DEFAULT 'X'.

include /fox/backup_time_dec_ii.


at selection-screen on value-request for p_path.

  lcl_main=>get_path( ).

start-of-selection.

  lcl_main=>start( ).



AND the Include: /FOX/BACKUP_TIME_DEC_II

*&---------------------------------------------------------------------*
*&  Include          From AbapFox - www.Abapfox.org
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
*       CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_main definition.

  public section.

    class-methods: start,
                   generate_dyn_structure,
                   gen_xml_upload importing i_name type any
                                            i_tab  type any,
                   gen_xml_download importing i_name type any,
                   get_path,
                   load_xml.


    types:   begin of ty_xml_tab,
           d type dcxmllines,
      end of ty_xml_tab.

    class-data: mo_data type ref to data.
    class-data: mo_me type ref to lcl_main.


endclass.                    "lcl_main DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_main implementation.


  method start.

    loop at s_class into s_class.

      create object mo_me.
      call method mo_me->generate_dyn_structure( ).
      clear mo_me.

    endloop.

  endmethod.                    "start


  method generate_dyn_structure.

    "-->Pool------------------------------------------------#####"
    data:
      t_tadir type standard table of tadir,
      w_tadir type tadir,
      l_string type string,
      l_lines type i.
    field-symbols: <dinamic_data> type standard table.
    "-->Pool------------------------------------------------#####"

    select * from tadir into table t_tadir
     where devclass = s_class-low
       and object = 'TABL'.

    if sy-subrc <> 0.
      leave program.
    endif.

    loop at t_tadir into w_tadir.

      "-->Check object is a table
      try .
          select count(*) from (w_tadir-obj_name)
            up to 1 rows.
        catch cx_sy_dynamic_osql_semantics.
          continue.
      endtry.

      l_string = w_tadir-obj_name.

      do 5 times.
        replace '/' with '_@_' into l_string.
      enddo.

      create data mo_data type standard table of (w_tadir-obj_name).
      assign mo_data->* to <dinamic_data>.

      check sy-subrc = 0.

      "-->Seleciondando os dados da tabela
      try .
          select * from (w_tadir-obj_name)
            into table <dinamic_data>.

        catch cx_sy_dynamic_osql_semantics.
          continue.
      endtry.

   

      "-->Criando o XML
      case 'X'.
        when c_do.

 check <dinamic_data>[] is not initial.
          call method mo_me->gen_xml_download( l_string ).
        when c_up.
          call method mo_me->gen_xml_upload( i_name = l_string i_tab = w_tadir-obj_name ).
      endcase.
    endloop.

  endmethod.                    "generate_dyn_structure

  method gen_xml_upload.


    "-->POOL------------------------------------------------------------------------"
    data:  l_filename(200) type c.
    data: l_filename_local type string.

    data: l_dom type ref to if_ixml_element,
                  m_document type ref to if_ixml_document,
                  g_ixml type ref to if_ixml,
                  w_string type xstring,
                  l_size type i,
                  w_result type i,
                  w_line type string,
                  it_xml type dcxmllines,
                  s_xml like line of it_xml,
                  w_rc type sy-subrc.

    data: xml type dcxmllines.
    data: rc type sy-subrc.


    types:   begin of ty_xml_tab,
           d type line of dcxmllines,
      end of ty_xml_tab.

    data: t_xml_tab type standard table of ty_xml_tab.
    data: w_xml_tab type ty_xml_tab.

    field-symbols: <dinamic_data> type standard table.
    data: l_name type string.
    "-->POOL------------------------------------------------------------------------"

    assign mo_data->* to <dinamic_data>.

    check sy-subrc = 0.

    l_name = i_name.

    concatenate
          p_path
          '\'
          l_name
          '.xml' into l_filename_local.

    call method cl_gui_frontend_services=>gui_upload
      exporting
        filename   = l_filename_local
        filetype   = 'BIN'
      importing
        filelength = l_size
      changing
        data_tab   = it_xml
      exceptions
        others     = 24.

    check sy-subrc = 0.

    do 5 times.
      replace '_@_' with '\' into l_name.
    enddo.

    class cl_ixml definition load.
    g_ixml = cl_ixml=>create( ).
    check not g_ixml is initial.
    m_document = g_ixml->create_document( ).
    check not m_document is initial.

    call function 'SDIXML_XML_TO_DOM'
      exporting
        xml           = it_xml
        size          = l_size
      importing
        document      = m_document
      exceptions
        invalid_input = 1
        others        = 2.

    l_dom ?= m_document->get_first_child( ).

    call function 'SDIXML_DOM_TO_DATA'
      exporting
        data_as_dom    = l_dom
      importing
        dataobject     = <dinamic_data>
      exceptions
        illegal_object = 1
        others         = 2.


    "-->Update data!!! WARNING WARNING!

    delete from (i_tab).
    insert (i_tab) from table <dinamic_data>.

    if sy-subrc = 0.
      write: / 'Tabela ' , i_tab , 'Atualizada com sucesso'.
    else.
      write: / 'Tabela ' , i_tab , 'Não foi atualizada.'.
    endif.


  endmethod.                    "gen_xml_upload

  method gen_xml_download.

    "-->POOL------------------------------------------------------------------------"
    data:  l_filename(200) type c.
    data: l_filename_local type string.

    data: l_dom type ref to if_ixml_element,
                  m_document type ref to if_ixml_document,
                  g_ixml type ref to if_ixml,
                  w_string type xstring,
                  l_size type i,
                  w_result type i,
                  w_line type string,
                  it_xml type dcxmllines,
                  s_xml like line of it_xml,
                  w_rc type sy-subrc.

    data: xml type dcxmllines.
    data: rc type sy-subrc.


    types:   begin of ty_xml_tab,
           d type line of dcxmllines,
      end of ty_xml_tab.

    data: t_xml_tab type standard table of ty_xml_tab.
    data: w_xml_tab type ty_xml_tab.

    field-symbols: <dinamic_data> type any.
    "-->POOL------------------------------------------------------------------------"

    assign mo_data->* to <dinamic_data>.

    check sy-subrc = 0.

    class cl_ixml definition load.
    g_ixml = cl_ixml=>create( ).
    check not g_ixml is initial.
    m_document = g_ixml->create_document( ).
    check not m_document is initial.


    call function 'SDIXML_DATA_TO_DOM'
      exporting
        name         = 'DATAEXCHANGE'
        dataobject   = <dinamic_data>
      importing
        data_as_dom  = l_dom
      changing
        document     = m_document
      exceptions
        illegal_name = 1
        others       = 2.


    check not l_dom is initial.

    w_rc = m_document->append_child( new_child = l_dom ).

    call function 'SDIXML_DOM_TO_XML'
      exporting
        document      = m_document
      importing
        xml_as_string = w_string
        size          = l_size
      tables
        xml_as_table  = it_xml
      exceptions
        no_document   = 1
        others        = 2.


    loop at it_xml into w_xml_tab-d.
      append w_xml_tab to t_xml_tab.
    endloop.

    if p_local is initial.

      if p_path is initial.
        concatenate
                  '\\minisap\sapmnt\gee\'
*              sy-datum
*              '_'
                  i_name
                  '.xml' into l_filename.
      else.
        concatenate
     p_path
*              sy-datum
*              '_'
     i_name
     '.xml' into l_filename.
      endif.

      call function 'SCMS_DOWNLOAD'
        exporting
          filename = l_filename
          filesize = l_size
          binary   = 'X'
          frontend = ' '
        tables
          data     = it_xml
        exceptions
          error    = 1
          others   = 2.


      if sy-subrc <> 0.
        write: 'Error no download!!!'.
      endif.

    else.

      concatenate
               p_path
               '\'
               i_name
               '.xml' into l_filename_local.

      call method cl_gui_frontend_services=>gui_download
        exporting
          bin_filesize = l_size
          filename     = l_filename_local
          filetype     = 'BIN'
        changing
          data_tab     = it_xml
        exceptions
          others       = 24.
    endif.


  endmethod.                    "gen_xml

  method load_xml.


    "-->POOL------------------------------------------------------------------------"
    data:  l_filename type rlgrap-filename value 'c:\temp\texte.xml'.

    data: l_dom type ref to if_ixml_element,
                  m_document type ref to if_ixml_document,
                  g_ixml type ref to if_ixml,
                  w_string type xstring,
                  l_size type i,
                  w_result type i,
                  w_line type string,
                  it_xml type dcxmllines,
                  s_xml like line of it_xml,
                  w_rc type sy-subrc.

    data: xml type dcxmllines.
    data: rc type sy-subrc.


    types:   begin of ty_xml_tab,
           d type line of dcxmllines,
      end of ty_xml_tab.




    data: t_xml_tab type standard table of ty_xml_tab.
    data: w_xml_tab type ty_xml_tab.

    field-symbols: <dinamic_data> type any.
    "-->POOL------------------------------------------------------------------------"

    assign mo_data->* to <dinamic_data>
  .
  endmethod.                    "load_xml

  method get_path.

    data: l_selected_folder type string.

    call method cl_gui_frontend_services=>directory_browse
      changing
        selected_folder      = l_selected_folder
      exceptions
        cntl_error           = 1
        error_no_gui         = 2
        not_supported_by_gui = 3
        others               = 4.

    if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
                 with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.

    p_path = l_selected_folder.

  endmethod.                    "get_path

endclass.                    "lcl_main IMPLEMENTATION




Gostou da publicação? Então clica na raposinha e curta nossa fanpage \o/


Quer aprender ABAP definitivamente, sem precisar sair de casa?

Acesse: www.abapfox.org


0 comentários:

SAP ABAP DATA_TO_DO and DOM_TO_DATA. Transform Internal table into XML files and vice versa.




If you ever had to move data between environments or use the test environment data quality always found trouble doing this. So here follows a program to download and upload all tables per package.
For that i transform the internal table in XML files and then, the xml file back in the internal table.
The magic is in this two functions:

DATA_TO_DO and DOM_TO_DATA

Farewell!


/FOX/BACKUP_TIME_II

*&---------------------------------------------------------------------*
*& Report  /GEE/BACKUP_TIME
*& From AbapFox - www.Abapfox.org
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  /fox/backup_time_ii.

tables: tadir.

select-options: s_class for tadir-devclass.

parameters p_path type string obligatory.

parameters: c_do radiobutton group g1,
            c_up radiobutton group g1.
parameters: p_local as checkbox DEFAULT 'X'.

include /fox/backup_time_dec_ii.


at selection-screen on value-request for p_path.

  lcl_main=>get_path( ).

start-of-selection.

  lcl_main=>start( ).



AND the Include: /FOX/BACKUP_TIME_DEC_II

*&---------------------------------------------------------------------*
*&  Include          From AbapFox - www.Abapfox.org
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
*       CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_main definition.

  public section.

    class-methods: start,
                   generate_dyn_structure,
                   gen_xml_upload importing i_name type any
                                            i_tab  type any,
                   gen_xml_download importing i_name type any,
                   get_path,
                   load_xml.


    types:   begin of ty_xml_tab,
           d type dcxmllines,
      end of ty_xml_tab.

    class-data: mo_data type ref to data.
    class-data: mo_me type ref to lcl_main.


endclass.                    "lcl_main DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_main implementation.


  method start.

    loop at s_class into s_class.

      create object mo_me.
      call method mo_me->generate_dyn_structure( ).
      clear mo_me.

    endloop.

  endmethod.                    "start


  method generate_dyn_structure.

    "-->Pool------------------------------------------------#####"
    data:
      t_tadir type standard table of tadir,
      w_tadir type tadir,
      l_string type string,
      l_lines type i.
    field-symbols: <dinamic_data> type standard table.
    "-->Pool------------------------------------------------#####"

    select * from tadir into table t_tadir
     where devclass = s_class-low
       and object = 'TABL'.

    if sy-subrc <> 0.
      leave program.
    endif.

    loop at t_tadir into w_tadir.

      "-->Check object is a table
      try .
          select count(*) from (w_tadir-obj_name)
            up to 1 rows.
        catch cx_sy_dynamic_osql_semantics.
          continue.
      endtry.

      l_string = w_tadir-obj_name.

      do 5 times.
        replace '/' with '_@_' into l_string.
      enddo.

      create data mo_data type standard table of (w_tadir-obj_name).
      assign mo_data->* to <dinamic_data>.

      check sy-subrc = 0.

      "-->Seleciondando os dados da tabela
      try .
          select * from (w_tadir-obj_name)
            into table <dinamic_data>.

        catch cx_sy_dynamic_osql_semantics.
          continue.
      endtry.

   

      "-->Criando o XML
      case 'X'.
        when c_do.

 check <dinamic_data>[] is not initial.
          call method mo_me->gen_xml_download( l_string ).
        when c_up.
          call method mo_me->gen_xml_upload( i_name = l_string i_tab = w_tadir-obj_name ).
      endcase.
    endloop.

  endmethod.                    "generate_dyn_structure

  method gen_xml_upload.


    "-->POOL------------------------------------------------------------------------"
    data:  l_filename(200) type c.
    data: l_filename_local type string.

    data: l_dom type ref to if_ixml_element,
                  m_document type ref to if_ixml_document,
                  g_ixml type ref to if_ixml,
                  w_string type xstring,
                  l_size type i,
                  w_result type i,
                  w_line type string,
                  it_xml type dcxmllines,
                  s_xml like line of it_xml,
                  w_rc type sy-subrc.

    data: xml type dcxmllines.
    data: rc type sy-subrc.


    types:   begin of ty_xml_tab,
           d type line of dcxmllines,
      end of ty_xml_tab.

    data: t_xml_tab type standard table of ty_xml_tab.
    data: w_xml_tab type ty_xml_tab.

    field-symbols: <dinamic_data> type standard table.
    data: l_name type string.
    "-->POOL------------------------------------------------------------------------"

    assign mo_data->* to <dinamic_data>.

    check sy-subrc = 0.

    l_name = i_name.

    concatenate
          p_path
          '\'
          l_name
          '.xml' into l_filename_local.

    call method cl_gui_frontend_services=>gui_upload
      exporting
        filename   = l_filename_local
        filetype   = 'BIN'
      importing
        filelength = l_size
      changing
        data_tab   = it_xml
      exceptions
        others     = 24.

    check sy-subrc = 0.

    do 5 times.
      replace '_@_' with '\' into l_name.
    enddo.

    class cl_ixml definition load.
    g_ixml = cl_ixml=>create( ).
    check not g_ixml is initial.
    m_document = g_ixml->create_document( ).
    check not m_document is initial.

    call function 'SDIXML_XML_TO_DOM'
      exporting
        xml           = it_xml
        size          = l_size
      importing
        document      = m_document
      exceptions
        invalid_input = 1
        others        = 2.

    l_dom ?= m_document->get_first_child( ).

    call function 'SDIXML_DOM_TO_DATA'
      exporting
        data_as_dom    = l_dom
      importing
        dataobject     = <dinamic_data>
      exceptions
        illegal_object = 1
        others         = 2.


    "-->Update data!!! WARNING WARNING!

    delete from (i_tab).
    insert (i_tab) from table <dinamic_data>.

    if sy-subrc = 0.
      write: / 'Tabela ' , i_tab , 'Atualizada com sucesso'.
    else.
      write: / 'Tabela ' , i_tab , 'Não foi atualizada.'.
    endif.


  endmethod.                    "gen_xml_upload

  method gen_xml_download.

    "-->POOL------------------------------------------------------------------------"
    data:  l_filename(200) type c.
    data: l_filename_local type string.

    data: l_dom type ref to if_ixml_element,
                  m_document type ref to if_ixml_document,
                  g_ixml type ref to if_ixml,
                  w_string type xstring,
                  l_size type i,
                  w_result type i,
                  w_line type string,
                  it_xml type dcxmllines,
                  s_xml like line of it_xml,
                  w_rc type sy-subrc.

    data: xml type dcxmllines.
    data: rc type sy-subrc.


    types:   begin of ty_xml_tab,
           d type line of dcxmllines,
      end of ty_xml_tab.

    data: t_xml_tab type standard table of ty_xml_tab.
    data: w_xml_tab type ty_xml_tab.

    field-symbols: <dinamic_data> type any.
    "-->POOL------------------------------------------------------------------------"

    assign mo_data->* to <dinamic_data>.

    check sy-subrc = 0.

    class cl_ixml definition load.
    g_ixml = cl_ixml=>create( ).
    check not g_ixml is initial.
    m_document = g_ixml->create_document( ).
    check not m_document is initial.


    call function 'SDIXML_DATA_TO_DOM'
      exporting
        name         = 'DATAEXCHANGE'
        dataobject   = <dinamic_data>
      importing
        data_as_dom  = l_dom
      changing
        document     = m_document
      exceptions
        illegal_name = 1
        others       = 2.


    check not l_dom is initial.

    w_rc = m_document->append_child( new_child = l_dom ).

    call function 'SDIXML_DOM_TO_XML'
      exporting
        document      = m_document
      importing
        xml_as_string = w_string
        size          = l_size
      tables
        xml_as_table  = it_xml
      exceptions
        no_document   = 1
        others        = 2.


    loop at it_xml into w_xml_tab-d.
      append w_xml_tab to t_xml_tab.
    endloop.

    if p_local is initial.

      if p_path is initial.
        concatenate
                  '\\minisap\sapmnt\gee\'
*              sy-datum
*              '_'
                  i_name
                  '.xml' into l_filename.
      else.
        concatenate
     p_path
*              sy-datum
*              '_'
     i_name
     '.xml' into l_filename.
      endif.

      call function 'SCMS_DOWNLOAD'
        exporting
          filename = l_filename
          filesize = l_size
          binary   = 'X'
          frontend = ' '
        tables
          data     = it_xml
        exceptions
          error    = 1
          others   = 2.


      if sy-subrc <> 0.
        write: 'Error no download!!!'.
      endif.

    else.

      concatenate
               p_path
               '\'
               i_name
               '.xml' into l_filename_local.

      call method cl_gui_frontend_services=>gui_download
        exporting
          bin_filesize = l_size
          filename     = l_filename_local
          filetype     = 'BIN'
        changing
          data_tab     = it_xml
        exceptions
          others       = 24.
    endif.


  endmethod.                    "gen_xml

  method load_xml.


    "-->POOL------------------------------------------------------------------------"
    data:  l_filename type rlgrap-filename value 'c:\temp\texte.xml'.

    data: l_dom type ref to if_ixml_element,
                  m_document type ref to if_ixml_document,
                  g_ixml type ref to if_ixml,
                  w_string type xstring,
                  l_size type i,
                  w_result type i,
                  w_line type string,
                  it_xml type dcxmllines,
                  s_xml like line of it_xml,
                  w_rc type sy-subrc.

    data: xml type dcxmllines.
    data: rc type sy-subrc.


    types:   begin of ty_xml_tab,
           d type line of dcxmllines,
      end of ty_xml_tab.




    data: t_xml_tab type standard table of ty_xml_tab.
    data: w_xml_tab type ty_xml_tab.

    field-symbols: <dinamic_data> type any.
    "-->POOL------------------------------------------------------------------------"

    assign mo_data->* to <dinamic_data>
  .
  endmethod.                    "load_xml

  method get_path.

    data: l_selected_folder type string.

    call method cl_gui_frontend_services=>directory_browse
      changing
        selected_folder      = l_selected_folder
      exceptions
        cntl_error           = 1
        error_no_gui         = 2
        not_supported_by_gui = 3
        others               = 4.

    if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
                 with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.

    p_path = l_selected_folder.

  endmethod.                    "get_path

endclass.                    "lcl_main IMPLEMENTATION




Gostou da publicação? Então clica na raposinha e curta nossa fanpage \o/


Quer aprender ABAP definitivamente, sem precisar sair de casa?

Acesse: www.abapfox.org


0 comentários:

Copyright © 2013 ABAP SAP - AbapFox! Aprenda ABAP Definitivamente