Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

create xml tag dynamically within simple transformation

Former Member
0 Likes
1,937

Hi all together,

I'm faceing the following problem:

There is a internal table provided, consisting of name/value-pairs. Something like

ls_struc-name = 'NAME1'.

ls_struc-value = 'XYZ'.

append ls_struc to lt_struc

ls_struc-name = 'CITY1'.

ls_struc-value = 'Munich'.

append ls_struc to lt_struc

and so on.

I have to create this XML (with simple transformation):

<NAME1>XYZ</NAME1>

<CITY1>Munich</CITY1>

Everythings works fine so far (tables, values etc.), but I have problems with creating the XML-Tag, e.g <NAME1> from "$line.name" within the transformation. I don't have any information about the data structure (therefore "call transaction id" does not work).

Any hint is useful!!!

Best regards,

Thomas

2 REPLIES 2
Read only

Former Member
0 Likes
916

Exaple program for creating XML file from abap

REPORT  z_xit_xml_dom_create.

  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:\temp\flights.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.

Read only

Former Member
0 Likes
916

Hi Mahesh,

I wrote "within Simple Transformation". That means without building DOM.

Regards,

Thomas