quarta-feira, 20 de dezembro de 2017

[CODIGO] ABAP para XML

Tutorial de como realizar conversão de Tabela Interna no SAP para XML.
Clique aqui caso queira ver o procedimento inverso.

Programa (SE38):
*-------------------------------------------------------------------*
*  Report Z_ABAP_TO_XML                                             *
*                                                                   *
*-------------------------------------------------------------------*
*  Write the data from an internal ABAP table into an XML document, *
*  and write it onto your frontend computer                         *
*-------------------------------------------------------------------*

REPORT z_abap_to_xml.

TYPE-POOLS: abap.

CONSTANTS gs_file TYPE string VALUE ´C:\temp\customers.xml´.
* This is the structure for the data to go into the XML file

TYPES: BEGIN OF ts_person,
  cust_id(4)    TYPE n,
  firstname(20) TYPE c,
  lastname(20)  TYPE c,
END OF ts_person.

* Table for the XML content

DATA: gt_itab        TYPE STANDARD TABLE OF char2048.

* Table and work area for the data to fill the XML file with

DATA: gt_person      TYPE STANDARD TABLE OF ts_person,
      gs_person      TYPE ts_person.

* Source table that contains references
* of the internal tables that go into the XML file

DATA: gt_source_itab TYPE abap_trans_srcbind_tab,
      gs_source_wa   TYPE abap_trans_resbind.

* For error handling

DATA: gs_rif_ex      TYPE REF TO cx_root,
      gs_var_text    TYPE string.

* Fill the internal table

gs_person-cust_id   = ´3´.
gs_person-firstname = ´Bill´.
gs_person-lastname  = ´Gates´.
APPEND gs_person TO gt_person.

gs_person-cust_id   = ´4´.
gs_person-firstname = ´Frodo´.
gs_person-lastname  = ´Baggins´.
APPEND gs_person TO gt_person.

* Fill the source table with a reference to the data table.
* Within the XSLT stylesheet, the data table can be accessed with
* "IPERSON".

GET REFERENCE OF gt_person INTO gs_source_wa-value.
gs_source_wa-name = ´IPERSON´.
APPEND gs_source_wa TO gt_source_itab.

* Perform the XSLT stylesheet

TRY.

    CALL TRANSFORMATION z_abap_to_xml
    SOURCE (gt_source_itab)
    RESULT XML gt_itab.

  CATCH cx_root INTO gs_rif_ex.

    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE ´E´.

ENDTRY.

* Download the XML file to your client

CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    filename                = gs_file
  CHANGING
    data_tab                = gt_itab
  EXCEPTIONS
    file_write_error        = 1
    no_batch                = 2
    gui_refuse_filetransfer = 3
    invalid_type            = 4
    no_authority            = 5
    unknown_error           = 6
    header_not_allowed      = 7
    separator_not_allowed   = 8
    filesize_not_allowed    = 9
    header_too_long         = 10
    dp_error_create         = 11
    dp_error_send           = 12
    dp_error_write          = 13
    unknown_dp_error        = 14
    access_denied           = 15
    dp_out_of_memory        = 16
    disk_full               = 17
    dp_timeout              = 18
    file_not_found          = 19
    dataprovider_exception  = 20
    control_flush_error     = 21
    not_supported_by_gui    = 22
    error_no_gui            = 23
    OTHERS                  = 24.

IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Transformation (STRANS): <z_abap_to_xml>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <CUSTOMERS>
      <xsl:apply-templates select="//IPERSON/item"/>
    </CUSTOMERS>
  </xsl:template>

  <xsl:template match="IPERSON/item">
    <item>
      <customer_id>
        <xsl:value-of select="CUST_ID"/>
      </customer_id>
      <first_name>
        <xsl:value-of select="FIRSTNAME"/>
      </first_name>
      <last_name>
        <xsl:value-of select="LASTNAME"/>
      </last_name>
    </item>
  </xsl:template>

</xsl:transform>

XML: <customers.xml>

















Post Original

Nenhum comentário:

Postar um comentário