Skip to Content
avatar image
Former Member

XML to Internal Table and Vice-Versa

Hi,

I need to create a program that reads information from an XML file to an ABAP internal table, and vice-versa.

I've found this link, that explains how to create a transformation program to define the mappings bewteen the XML and the Internal Table. However, when I'm trying to create a transformation (PROCEDURE - Step 2), in SE80 no option to create a Transformation appears.

http://http://help.sap.com/saphelp_nw04/helpdata/en/fd/9d734b389211d596a200a0c94260a5/frameset.htmhttp://help.sap.com/saphelp_nw04/helpdata/en/fd/9d734b389211d596a200a0c94260a5/frameset.htm">http://http://help.sap.com/saphelp_nw04/helpdata/en/fd/9d734b389211d596a200a0c94260a5/frameset.htm>

Could you please help me to solve this problem?

Thanks in advance,

Nuno A.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    May 17, 2007 at 03:39 PM

    Hi nuno,

    1. itab --- > xml

    xml ---> itab.

    2. This program will do both.

    (just copy paste in new program)

    3.

    REPORT abc.

    *----


    DATA

    DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

    DATA : BEGIN OF itab OCCURS 0,

    a(100) TYPE c,

    END OF itab.

    DATA: xml_out TYPE string .

    DATA : BEGIN OF upl OCCURS 0,

    f(255) TYPE c,

    END OF upl.

    DATA: xmlupl TYPE string .

                                                                • FIRST PHASE

                                                                • FIRST PHASE

                                                                • FIRST PHASE

                                                                *----


                                                                Fetch Data

                                                                SELECT * FROM t001 INTO TABLE t001.

                                                                *----


                                                                XML

                                                                CALL TRANSFORMATION ('ID')

                                                                SOURCE tab = t001[]

                                                                RESULT XML xml_out.

                                                                *----


                                                                Convert to TABLE

                                                                CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'

                                                                EXPORTING

                                                                i_string = xml_out

                                                                i_tabline_length = 100

                                                                TABLES

                                                                et_table = itab.

                                                                *----


                                                                Download

                                                                CALL FUNCTION 'GUI_DOWNLOAD'

                                                                EXPORTING

                                                                filetype = 'BIN'

                                                                filename = 'd:\xx.xml'

                                                                TABLES

                                                                data_tab = itab.

                                                                                                                            • SECOND PHASE

                                                                                                                            • SECOND PHASE

                                                                                                                            • SECOND PHASE

                                                                                                                            BREAK-POINT.

                                                                                                                            REFRESH t001.

                                                                                                                            CLEAR t001.

                                                                                                                            CALL FUNCTION 'GUI_UPLOAD'

                                                                                                                            EXPORTING

                                                                                                                            filename = 'D:\XX.XML'

                                                                                                                            filetype = 'BIN'

                                                                                                                            TABLES

                                                                                                                            data_tab = upl.

                                                                                                                            LOOP AT upl.

                                                                                                                            CONCATENATE xmlupl upl-f INTO xmlupl.

                                                                                                                            ENDLOOP.

                                                                                                                            *----


                                                                                                                            XML

                                                                                                                            CALL TRANSFORMATION ('ID')

                                                                                                                            SOURCE XML xmlupl

                                                                                                                            RESULT tab = t001[]

                                                                                                                            .

                                                                                                                            BREAK-POINT.

                                                                                                                            regards,

                                                                                                                            amit m.

                                                                                                                            Add comment
                                                                                                                            10|10000 characters needed characters exceeded

                                                                                                                            • Former Member Former Member

                                                                                                                              In se80, Right click on your package name within the tree -> create -> other -> transformation.

                                                                                                                              If it doesn't work, which SP level have you?

                                                                                                                              Regards

                                                                                                                          • avatar image
                                                                                                                            Former Member
                                                                                                                            May 17, 2007 at 03:33 PM
                                                                                                                            Add comment
                                                                                                                            10|10000 characters needed characters exceeded

                                                                                                                          • avatar image
                                                                                                                            Former Member
                                                                                                                            May 17, 2007 at 03:39 PM

                                                                                                                            Hi,

                                                                                                                            TYPE-POOLS: ixml.
                                                                                                                            
                                                                                                                              TYPES: BEGIN OF xml_line,
                                                                                                                                      data(256) TYPE x,
                                                                                                                                     END OF xml_line.
                                                                                                                            
                                                                                                                              DATA: l_ixml            TYPE REF TO if_ixml,
                                                                                                                                    l_streamfactory   TYPE REF TO if_ixml_stream_factory,
                                                                                                                                    l_ostream         TYPE REF TO if_ixml_ostream,
                                                                                                                                    l_renderer        TYPE REF TO if_ixml_renderer,
                                                                                                                                    l_document        TYPE REF TO if_ixml_document.
                                                                                                                            
                                                                                                                              DATA: l_element_flights TYPE REF TO if_ixml_element,
                                                                                                                                    l_element_airline TYPE REF TO if_ixml_element,
                                                                                                                                    l_element_flight  TYPE REF TO if_ixml_element,
                                                                                                                                    l_element_from    TYPE REF TO if_ixml_element,
                                                                                                                                    l_element_to      TYPE REF TO if_ixml_element,
                                                                                                                                    l_element_dummy   TYPE REF TO if_ixml_element,
                                                                                                                                    l_value           TYPE string.
                                                                                                                            
                                                                                                                              DATA: l_xml_table       TYPE TABLE OF xml_line,
                                                                                                                                    l_xml_size        TYPE i,
                                                                                                                                    l_rc              TYPE i.
                                                                                                                            
                                                                                                                              DATA: lt_spfli          TYPE TABLE OF spfli.
                                                                                                                              DATA: l_spfli           TYPE spfli.
                                                                                                                            
                                                                                                                            
                                                                                                                              START-OF-SELECTION.
                                                                                                                            *   Fill the internal table
                                                                                                                                SELECT * FROM spfli INTO TABLE lt_spfli.
                                                                                                                            
                                                                                                                            *   Sort internal table
                                                                                                                                SORT lt_spfli BY carrid.
                                                                                                                            
                                                                                                                            *   Start filling xml dom object from internal table
                                                                                                                                LOOP AT lt_spfli INTO l_spfli.
                                                                                                                            
                                                                                                                                  AT FIRST.
                                                                                                                            *       Creating a ixml factory
                                                                                                                                    l_ixml = cl_ixml=>create( ).
                                                                                                                            *       Creating the dom object model
                                                                                                                                    l_document = l_ixml->create_document( ).
                                                                                                                            *       Fill root node with value flights
                                                                                                                                    l_element_flights  = l_document->create_simple_element(
                                                                                                                                                name = 'flights'
                                                                                                                                                parent = l_document ).
                                                                                                                                  ENDAT.
                                                                                                                            
                                                                                                                                  AT NEW carrid.
                                                                                                                            *       Create element 'airline' as child of 'flights'
                                                                                                                                    l_element_airline  = l_document->create_simple_element(
                                                                                                                                                name = 'airline'
                                                                                                                                                parent = l_element_flights  ).
                                                                                                                            
                                                                                                                            *       Create attribute 'code' of node 'airline'
                                                                                                                                    l_value = l_spfli-carrid.
                                                                                                                                    l_rc = l_element_airline->set_attribute( name = 'code' value =
                                                                                                                            l_value ).
                                                                                                                            
                                                                                                                            *       Create attribute 'name' of node 'airline'
                                                                                                                                    SELECT SINGLE carrname FROM scarr INTO l_value WHERE carrid EQ
                                                                                                                            l_spfli-carrid.
                                                                                                                                    l_rc = l_element_airline->set_attribute( name = 'name' value =
                                                                                                                            l_value ).
                                                                                                                            
                                                                                                                                  ENDAT.
                                                                                                                            
                                                                                                                                  AT NEW connid.
                                                                                                                            *       Create element 'flight' as child of 'airline'
                                                                                                                                    l_element_flight  = l_document->create_simple_element(
                                                                                                                                                name = 'flight'
                                                                                                                                                parent = l_element_airline  ).
                                                                                                                            
                                                                                                                            *       Create attribute 'number' of node 'flight'
                                                                                                                                    l_value = l_spfli-connid.
                                                                                                                                    l_rc = l_element_flight->set_attribute( name = 'number' value =
                                                                                                                            l_value ).
                                                                                                                            
                                                                                                                                  ENDAT.
                                                                                                                            
                                                                                                                            *     Create element 'from' as child of 'flight'
                                                                                                                                  CONCATENATE l_spfli-cityfrom ',' l_spfli-countryfr INTO l_value.
                                                                                                                                  l_element_from  = l_document->create_simple_element(
                                                                                                                                              name = 'from'
                                                                                                                                              value = l_value
                                                                                                                                              parent = l_element_flight  ).
                                                                                                                            
                                                                                                                            *     Create attribute 'airport' of node 'from'
                                                                                                                                  l_value = l_spfli-airpfrom.
                                                                                                                                  l_rc = l_element_from->set_attribute( name = 'airport' value =
                                                                                                                            l_value ).
                                                                                                                            
                                                                                                                            *     Create element 'to' as child of 'flight'
                                                                                                                                  CONCATENATE l_spfli-cityto ',' l_spfli-countryto INTO l_value.
                                                                                                                                  l_element_to  = l_document->create_simple_element(
                                                                                                                                              name = 'to'
                                                                                                                                              value = l_value
                                                                                                                                              parent = l_element_flight  ).
                                                                                                                            
                                                                                                                            *     Create attribute 'airport' of node 'from'
                                                                                                                                  l_value = l_spfli-airpto.
                                                                                                                                  l_rc = l_element_to->set_attribute( name = 'airport' value =
                                                                                                                            l_value ).
                                                                                                                            
                                                                                                                            *     Create element 'departure' as child of 'flight'
                                                                                                                                  l_value = l_spfli-deptime.
                                                                                                                                  l_element_dummy  = l_document->create_simple_element(
                                                                                                                                              name = 'departure'
                                                                                                                                              value = l_value
                                                                                                                                              parent = l_element_flight ).
                                                                                                                            
                                                                                                                            *     Create element 'arrival' as child of 'flight'
                                                                                                                                  l_value = l_spfli-arrtime.
                                                                                                                                  l_element_dummy  = l_document->create_simple_element(
                                                                                                                                              name = 'arrival'
                                                                                                                                              value = l_value
                                                                                                                                              parent = l_element_flight ).
                                                                                                                            
                                                                                                                            *     Create element 'type' as child of 'flight'
                                                                                                                                  CASE l_spfli-fltype.
                                                                                                                                    WHEN 'X'.
                                                                                                                                      l_value = 'Charter'.
                                                                                                                                    WHEN OTHERS.
                                                                                                                                      l_value = 'Scheduled'.
                                                                                                                                  ENDCASE.
                                                                                                                                  l_element_dummy  = l_document->create_simple_element(
                                                                                                                                              name = 'type'
                                                                                                                                              value = l_value
                                                                                                                                              parent = l_element_flight ).
                                                                                                                                ENDLOOP.
                                                                                                                                IF sy-subrc NE 0.
                                                                                                                            *      MESSAGE 'No data into db table ''spfli'', please run program
                                                                                                                            *''SAPBC_DATA_GENERATOR'' with transaction ''SA38''' TYPE 'E'.
                                                                                                                                ENDIF.
                                                                                                                            
                                                                                                                            
                                                                                                                            *   Creating a stream factory
                                                                                                                                l_streamfactory = l_ixml->create_stream_factory( ).
                                                                                                                            *   Connect internal XML table to stream factory
                                                                                                                                l_ostream = l_streamfactory->create_ostream_itable( table =
                                                                                                                            l_xml_table ).
                                                                                                                            
                                                                                                                            *   Rendering the document
                                                                                                                                l_renderer = l_ixml->create_renderer( ostream  = l_ostream
                                                                                                                                                                      document = l_document ).
                                                                                                                                l_rc = l_renderer->render( ).
                                                                                                                            
                                                                                                                            *   Saving the XML document
                                                                                                                                l_xml_size = l_ostream->get_num_written_raw( ).
                                                                                                                            
                                                                                                                                CALL METHOD cl_gui_frontend_services=>gui_download
                                                                                                                                  EXPORTING
                                                                                                                                    bin_filesize = l_xml_size
                                                                                                                                    filename     = 'C:tempflight.xml'
                                                                                                                                    filetype     = 'BIN'
                                                                                                                                  CHANGING
                                                                                                                                    data_tab     = l_xml_table
                                                                                                                                  EXCEPTIONS
                                                                                                                                    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.

                                                                                                                            This Code is available on SDN Please search the same.

                                                                                                                            Regards,

                                                                                                                            Ranjit Thakur.

                                                                                                                            <b>Please Mark The Helpful Answer.</b>

                                                                                                                            Add comment
                                                                                                                            10|10000 characters needed characters exceeded