Sorting internal table

Hi all,
I have a scenario wherein i have to sort the internal table without using the sort function( i.e sort syntax).Is there any logic.
Can nebody pls help me out.
Regards
Jak

Hai,
To understand bubble sort, think of an air bubble rising in water
To sort N items, N passes are made through the data.
The result of the first pass is that the smallest item is in the last location of the array.
The result of the second pass is that the second smallest item is in the second last location of the array.
etc.
After N passes, the entire array is sorted.
The operation in each pass is as follows:
First, the values in the first two locations are compared. If necessary the values are exchanged, so that the smaller one is last.
Then, the values in the second and third locations are compared. If necessary the values are exchanged, so that again the smaller one is last.
This process repeats to the end of the array.
In effect, the smaller item bubbles its way towards the top. It keeps on going until either it reaches the top (and the pass ends), or it strikes a smaller item. In that case it stops itself, and gives that smaller item a nudge to start it on its way.
If a complete pass is made without any exchanges being made, the data must already be sorted. This is easily checked. Thus it might be possible to halt the algorithm without going through all N passes.

Similar Messages

  • How to find min and max of a field from sorted internal table

    Hi,
    I have sorted Internal Table by field f1.
    How do I find max and min value of f1.
    For min value of f1 I am using,
    READ TABLE IT1 INDEX 1.
    IT1-F1 = MIN.
    Is this correct? And how do I find the max value of f1 from this table.
    Thanks,
    CD

    Yes, that is right, and you can get the max like this.
    data: lv_lines type i.
    * get min
    READ TABLE IT1 INDEX 1.
    MIN  = IT1-F1.
    * get max
    lv_lines = lines( it1 ).
    read table it1 index lv_lines.
    MAX  = IT1-F1.
    Regards,
    Rich Heilman

  • Sorting internal table by date

    Hello experts,
                            i wat to sort internal table in asscending order and am using the code as follows,
                APPEND wa_final TO it_final.
                sort it_final by edatu vbeln  ASCENDING.
    am getting output as 05.09.2008
                                   10.09.2008
                                    27.08.2008
    but i want in 27.08.2008
                      05.09.208
                      10.09.2008
    please help me ASAP.
    Thanks,
    vino....

    Hi
    I think you uploaded date field as your output format. If it is in ddmmyyyy format, when you sort it, automatically it will sort. Check your input entries.
    Otherwise, what you had written is correct.
    check your code
    Regards,
    Dhanunjaya Reddy.

  • SORT INTERNAL TABLE USING NON KEY OF THE INTERNAL TABLE

    HI,
    i have one query for the cdpos table of abap.
    What i am looking is i want the latest CHANENR of the cdpos for that
    objectid  and tabname and fname in my internal table.
    what i thought of doing was to sort my internal table with objectid changnr tabname fname in descending order
    and than use delete adjacent duplicates command
    BUt that is not sorting based on all field , its sorting on changnr only
    please let me know what can i do

    no, that is not what i am looking at,
    I am saying say i have internal table cdpos
    OBJECTCLAS      OBJECTID    CHANGENR   TABNAME                        FNAME
    RECN_RECN      |0100TEST1   0000384409|    VICN01                        |RECNEND
    RECN_RECN       0100TEST1   0000383462    VICN01                          RECNLIFNR 
    RECN_RECN       0100TEST1    0000360190   VICN01                         RECNEND
    RECN_RECN        0100TEST1   0000340630    VICN01                         RECNEND
    this is my debug output after sorting on objectid changenr tabname fname in descending
    i wanted line 2nd to be at last so when i use delete adjacent command i can get 2 line item i.e 1st and 2nd line ...

  • Hashed & sorted internal tables

    hi all,
        can any body help me to understand the concept of <b>hashed & sorted</b> internal table  & how they can be used for improve the performance of report.
    regards
    Deepak

    Hi Deepak,
    If you have an internal table in your program which is used solely for lookup, it is good programming practice to use a hash table. The example below shows this, in combination with a method for buffering SELECT SINGLE results.
    Code
    *&      Form  select_dispo
          Get MRP controller and in-house production time from material
          and plant
         --> MATNR  Material
         --> RESWK  Plant
         <-- DISPO  MRP controller
         <-- DZEIT  In-house production time
    form select_from_marc using matnr werks dispo dzeit.
      types: begin of mrp_lookup_type,
               matnr like marc-matnr,
               werks like marc-werks,
               dispo like marc-dispo,
               dzeit like marc-dzeit,
             end of mrp_lookup_type.
    Define static hashed table to hold results
      statics: st_mrp type hashed table of mrp_lookup_type
                      with unique key matnr werks.
      data: l_wa_mrp type mrp_lookup_type.
      clear dzeit.
    See if data is in the table
      read table st_mrp into l_wa_mrp with table key matnr = matnr
                                                     werks = werks.
    If not in table, get it from the database
      if not sy-subrc is initial.
        select single dispo dzeit from marc
            into corresponding fields of l_wa_mrp-dispo
            where matnr eq matnr
              and werks eq werks.
    Insert into table
        l_wa_mrp-matnr = matnr.
        l_wa_mrp-werks = werks.
        insert l_wa_mrp into table st_mrp.
      endif.
      dispo = l_wa_mrp-dispo.                      " MRP Controller
      dzeit = l_wa_mrp-dzeit.                      " Inhouse production time
    endform.                    " select_from_marc

  • How to sort internal table

    Hi
    How to sort internal by binary search...please don't suggest SORT TABLE or HASHED table to use...

    hi,
    Use SORT Statement....
    i.e,
    sort itab.
    Here is the code
    TYPES: BEGIN OF PERSON_TYPE,
    NAME(10) TYPE C,
    AGE TYPE I,
    COUNTRY(3) TYPE C,
    END OF PERSON_TYPE.
    DATA: PERSON TYPE STANDARD TABLE OF PERSON_TYPE WITH
    NON-UNIQUE DEFAULT KEY INITIAL SIZE 5,
    WA_PERSON TYPE PERSON_TYPE.
    WA_PERSON-NAME = 'Muller'. WA_PERSON-AGE = 22.
    WA_PERSON-COUNTRY = 'USA'.
    APPEND WA_PERSON TO PERSON.
    WA_PERSON-NAME = 'Moller'. WA_PERSON-AGE = 25.
    WA_PERSON-COUNTRY = 'FRG'.
    APPEND WA_PERSON TO PERSON.
    WA_PERSON-NAME = 'Möller'. WA_PERSON-AGE = 22.
    WA_PERSON-COUNTRY = 'USA'.
    APPEND WA_PERSON TO PERSON.
    WA_PERSON-NAME = 'Miller'. WA_PERSON-AGE = 23.
    WA_PERSON-COUNTRY = 'USA'.
    APPEND WA_PERSON TO PERSON.
    SORT PERSON.
    SORT PERSON BY NAME AS TEXT
    SORT PERSON DESCENDING BY COUNTRY AGE NAME.
    SORT PERSON DESCENDING BY AGE ASCENDING NAME AS TEXT.
    Reward if it helps,
    Regards,
    Santosh

  • How to sort internal tables of unknown structure?

    Hello
    Itab is an internal table of unknown structure with field F1 and many other.
    How to sort this table by field F1 is the structure is unknown?
    SORT ITAB by F1 ascending.
    Thanks

    Hello,
    Which version of SAP are you using?
    As of Release 701, you can specify the fields dynamically by specifying the fields in an internal table of type ABAP_SORTORDER_TAB. Further read can be found here: [SORT by (otab)|http://help.sap.com/abapdocu_702/en/abapsort_itab.htm#!ABAP_ADDITION_5@5@].
    For prior releases you can try something like this:
    SORT itab BY ('F1') ('F2) ... ('Fn')
    Hope this helps.
    BR,
    Suhas

  • Sort Internal table based on another internal table

    Hello All,
    My Requirement is :
    I have two internal tables itab1 and itab2. itab1 has partner_number along with some more fields/columns. Based on the partner_numbers of the itab1 i write a query to get the values for partner_last_name and central_block_flag from the table but000 and i fill these values into a new itab2.
    Sort itab2 by partner_last_name and central_block.
    Now how do I sort itab1 based on the sorted itab2-partner_number. I need to have itab1 sorted similar to the order in the itab2.
    How do i do this?
    Please let me know if you need more details.Looking forward for your support.
    Regards,
    Harish P M

    Hello Micky,
    Thank you for your early reply.
    I have this scenario wherein i need to sort a existing internal table itab1 of type 'bapibus1006_relations' based on the 'Partner last name ' and 'Inactive/Active Flag'. These two fields are not available in this itab1 and so i need to create itab2 after a query on table but000.
    Sorting itab2 is , first sort itab2 based on the 'Active flag' and then sort this itab2 again by 'Partner last name'(ascending order).
    Finally when i get my sorted itab2 i would have to ensure that my objective of sorting itab1 is obtained.
    I guess i sound better now.
    Would be helpful if you could reply.
    Regards,
    Harish P M

  • Sorting internal table with line types

    Hi,
    I have internal table juts for an example with four entries as below:
    A[]
    Line.....Line Types
    444001.....P
    New York...C 
    Evershine..B
    Mary.......N
    I want to sort the internal table with line types in order of lin types NBCP.
    How can i do this ?
    Please help.
    Note: Above example is just with four entries, there might be internal table with ten entries and ten defined sorting order for line types.

    Hi Tushar,
    Please note that we can't changes the mandatory sequence of the address format as defined in the country specif address format. We can change the sequence of others fields by line_priority.
    Try to change the address type :
    ADDRESS_TYPE - Address type (from 3.0C)
    There are three types of address:
    Address type '1': addresses of firms or organizations; the address structure which is used in most SAP applications as 'Address'.
    Address type '2': address of a person
    Address type '3': work address, usually the address of a contact person in a company
    Please use T/code "OY01"> Select Country> Example :AU"--> Display --> Select address key and press F1. You will find SAP 's Address format configuration .
    sap help:
    Formatting Routine Key for Printing Addresses
    For printing addresses, there are country-specific routines which in each case copy the correct postal formatting of the address.
    The three-character "Address Layout Key" for the recipient country controls which of the routines available for formatting addresses in the relevant country is used.
    These routines are programmed into the ADDRESS_INTO_PRINTFORM function module.
    They are based on different national and international guidelines and norms, including:
    ISO 11180,
    contracts of the World Postal Union (Seoul 1994),
    international address samples from the World Postal Union
    as well as the available rules of the individual countries.
    Below you will find an overview of the country-specific formats currently implemented.
    Customers can program their own formatting routines using a customer exit. The SZAD0001 SAP enhancement has been defined in the package SZAD for this (-> transaction CMOD).
    General formatting rules
    The following parameters are used depending on the transaction:
    whether the company address (street/house number) or the P.O. Box address is printed if both exist,
    how many lines are available for printing,
    which is the sender country.
    If there are not enough lines, then lines are left out according to a standard sequence.
    The address format depends on whether the sender country is the same as, or different from, the recipient country. The country is always specified from abroad, either as a text name, or as an identification code of up to three characters (license plate code or country key).
    Where the country name is written out in full, it is written in the language of the sender country. If a language is not specified for the sender country, or the sender country itself is not specified, the logon language is used instead.
    Exception: the language for the country code can be explicitly overwritten by a parameter in the print program (e.g. when the country code for customs transit papers is always to be specified in a particular language, such as English). if the "Print country name in recipient language" flag is set in the print program, the recipient language is used.
    Other language-dependent components such as the title and the word 'PO Box' are printed in the recipient language or the recipient country language. If this cannot be determined, the logon language is again used.
    For all formats except Great Britain (006), Japan (013), and South Korea (017), the core of the address is formatted as follows (without empty lines, except for the compulsory empty line:)
    title line (if applicable)
    name block (differs depending on the address type, see below)
    street address or PO Box
                            (compulsory empty line, if applicable)
    city line with postal code
    country code (if applicable)
    The city line and the country name are always printed in upper case for foreign addresses (only for the complete address, not for short forms).
    The name block generally consists of the following:
    "Normal" addresses (address type SPACE and address type 1):
    NAME1
    NAME2
    NAME3
    NAME4
    Personal addresses (address type 2):
    Title of person and name of person
    Business address with department and contact person (address type 3):
    NAME1
    NAME2
    NAME3
    NAME4
    department
    title of person and name of person
    In addresses entered using Business Address Services (central address management) (see Release notes Central Address Management for Release 4.0 and Central Address Management for Release 4.5 ), the street address can comprise several lines (see Print street address), otherwise street and house number are maintained in the Street field.
    Some countries do not have a compulsory empty line (see notes below). The city and district are printed in the city line, connected by a hyphen (exceptions: 004 USA, 006 Great Britain/Ireland, 013 Japan, 015 Germany, 017 South Korea, 019 Denmark), provided that the total length does not exceed 35 characters. If a different city is specified for the PO Box (PFORT), this is used in the PO Box address.
    In all formats which use a country code (currently 001 European standard format 002 Italy, 011 Switzerland and 014 Austria ), the license plate code for that country is used. If this is not maintained, the country key in table T005 is used.
    If the "Print country name in foreign addresses" flag is set for the sender country in table 005, the country code is not used; the country name is printed in the last line of the address.
    Formats 004 (USA), 005 (Canada) and 008 (Singapore) contain a line ('F') for the function of the contact person in the company (if address type = '3'). This line comes immediately after the line 'N' (Name (and title) of the natural person).
    In formats 002 (Italy), 004 (USA), 005 (Canada), 006 (Great Britain), 007 (Brazil) and Australia (009), the REGIO field (Region, State, Province, County) is formatted. For the USA, Canada, Brazil and Australia, the key from table T005S is used; for Great Britain the text name from table T005U.
    In all countries for which no "Address structure key" is maintained, a standard format is used which corresponds to format "010".
    Hope this may help you.
    Lanka

  • Sorting internal table with variable no of fields

    Hi ,
    I want to sort the internal table with variable no of fields . Every time the  report is run the number of fields as well as the sequence by which the table has to be sorted changes . How to do this .
    regards

    Hi,
    Please try this.
    DATA: BEGIN OF ITAB OCCURS 0,
            F1(4),
          END OF ITAB.
    DATA: BEGIN OF ITAB2 OCCURS 0,
            F1(1),
            F2(1),
            F3(4),
          END OF ITAB2.
    ITAB-F1 = '01AC'.
    APPEND ITAB.
    ITAB-F1 = '02AB'.
    APPEND ITAB.
    ITAB-F1 = '01CD'.
    APPEND ITAB.
    ITAB-F1 = '02CA'.
    APPEND ITAB.
    LOOP AT ITAB.
      ITAB2-F1 = ITAB-F1+2(1).
      ITAB2-F2 = ITAB-F1+3(1).
      ITAB2-F3 = ITAB-F1.
      APPEND ITAB2.
    ENDLOOP.
    SORT ITAB2 BY F1 ASCENDING
                  F2 DESCENDING.
    LOOP AT ITAB2.
      WRITE: / ITAB2-F3.
    ENDLOOP.
    REWARD POINTS IF HELPFUL

  • Sorting internal table isue

    Hello,
    I am having problems sorting a table.
    The internal table has following data:
    Doc_num..........ver_num
    TEST123.............02
    TEST123.............01
    TESTXYZ.............02
    TESTXYZ.............03
    TESTXYZ.............01
    TESTXYZ.............04
    Now I am using statement :
    SORT itab by doc_num ver_num.
    Now this does sort the data according to correct doc_num but not according to ver_num.
    Please help.
    Thanks.
    Regards,
    Rajesh

    Note: I have defined doc_num and ver_num s string....
    My input is:
    TEST123.............1
    TEST123.............2
    TESTXYZ.............1
    TESTXYZ.............20
    TESTXYZ.............10
    TESTXYZ.............2
    I am seeing following:
    TEST123.............1
    TEST123.............1
    TESTXYZ.............1
    TESTXYZ.............10
    TESTXYZ.............20
    TESTXYZ.............2
    I want to see as follows:
    TEST123.............1
    TEST123.............2
    TESTXYZ.............1
    TESTXYZ.............2
    TESTXYZ.............10
    TESTXYZ.............20

  • Sorting a internal table

    hai all
    what is the effect of sorting the sorted internal table.
    thanks in advance

    Hello Sathish,
    If u declare an itab as sorted u cannot use the syntax for that itab.
    If useful reward.
    Vasanth

  • Differences between Standard , sorted and hashed internal tables

    Can any body please tell me what are the main Differences between
    1) <b>Standard internal table</b>
    2) <b>Hashed internal table</b>
    3) <b>Sorted internal table</b>
    Please give me a clear idea about these Three.
    Thanks
    Prabhudutta<b></b>

    Hi,
    <b>Standard Internal Tables</b>
    Standard tables have a linear index. You can access them using either the index or the key. If you use the key, the response time is in linear relationship to the number of table entries. The key of a standard table is always non-unique, and you may not include any specification for the uniqueness in the table definition.
    This table type is particularly appropriate if you want to address individual table entries using the index. This is the quickest way to access table entries. To fill a standard table, append lines using the (APPEND) statement. You should read, modify and delete lines by referring to the index (INDEX option with the relevant ABAP command).  The response time for accessing a standard table is in linear relation to the number of table entries. If you need to use key access, standard tables are appropriate if you can fill and process the table in separate steps. For example, you can fill a standard table by appending records and then sort it. If you then use key access with the binary search option (BINARY), the response time is in logarithmic relation to
    the number of table entries.
    <b>Sorted Internal Tables</b>
    Sorted tables are always saved correctly sorted by key. They also have a linear key, and, like standard tables, you can access them using either the table index or the key. When you use the key, the response time is in logarithmic relationship to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique, or non-unique, and you must specify either UNIQUE or NON-UNIQUE in the table definition.  Standard tables and sorted tables both belong to the generic group index tables.
    This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it. You fill the table using the (INSERT) statement, according to the sort sequence defined in the table key. Table entries that do not fit are recognised before they are inserted. The response time for access using the key is in logarithmic relation to the number of
    table entries, since the system automatically uses a binary search. Sorted tables are appropriate for partially sequential processing in a LOOP, as long as the WHERE condition contains the beginning of the table key.
    <b>Hashed Internal Tables</b>
    Hashes tables have no internal linear index. You can only access hashed tables by specifying the key. The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition.
    This table type is particularly suitable if you want mainly to use key access for table entries. You cannot access hashed tables using the index. When you use key access, the response time remains constant, regardless of the number of table entries. As with database tables, the key of a hashed table is always unique. Hashed tables are therefore a useful way of constructing and
    using internal tables that are similar to database tables.
    Regards
    Sudheer

  • Insert specific line in an internal table at a specific place

    Hi
    i have an internal table (itab) let's say
    Name1 Name2 NUM1 NUM2 NUM3
    Which already populater with data.
    i have to do a subtotal for every NAME1 and insert a line just after the NAME1 in the internal table
    and another subtotal by every NAME1 NAME2 and insert a line just after every NAME1 and NAME2 in the internal table
    then another Grand total at the end of the table
    My psudeo code is a bit like that but the insert is done in the wrong row
    Index=1
    LOOP at itab index
    if itab-name1 NE old
      insert structure in itab index
    elseif itab-name1 NE old and itab-name2 NE old
      insert structure in itab index
    elseif itab EQ last line
      insert structure in itab index
    else
      structure-num1 = structure-num1 + itab-num1
      structure-num2 = structure-num2 + itab-num2
    endif
    index = index + 1
    endloop
    Do you think i need to use a temporary table here. can u guide me in code plz..

    sort internal table by name1 and name2. <b>Have name1 and name2 as the first two variables in the internal table</b>
    declare a work area and an internal table with the same structure as internal table.
    declare num1 as type itab-num1.
    num2 and i_num2 type itab-num2.
    sort itab by name1 and name2.
    loop the internal itab1.
    append itab1 to itab2.
    wa-num1 = structure-num1 + itab-num1
    wa-num2 = structure-num2 + itab-num2.
    i_num2 = i_num2 + itab-num2.
    num1 = num1 + itab-num1.
    num2 = num2 + itab-num2.
    at end of name2.
    append wa to itab2.
    clear wa_name2.
    endat.
    at end of name1.
    wa-num2 = i_num2.
    append wa to itab2.
    clear: i_num2, wa.
    endat.
    at last.
    wa-num1 = num1.
    wa-num2 = num2.
    append wa_itab2.
    endat.
    endloop.
    Thanks.

  • Internal Table Data to XML

    Hi
    I have a requirement of writing internal table data to XML. Any idea where i should start.
    I have pretty good experience with ABAP and basic knowledge in XML.
    There are good blogs which talk about transformations and other stuff but they are not able to give me clear path to my solution.
    Could somebody give me a basic example or some reference material where i can move the data in internal table (assume Sales order details of a day) to XML.
    Thanks

    Refer the program -
    In this implementation we will only focus on the creation of the XML file and the transfer to the user. You can not create a XML document directly. You have to use a so called ixml factory first. 
    TYPE-POOLS: ixml.
    DATA: l_ixml TYPE REF TO if_ixml.
    l_ixml = cl_ixml=>create( ).
    This iXML factory can create an empty XML document object named l_document.
    DATA:  l_document TYPE REF TO if_ixml_document.
            l_document = l_ixml->create_document( ).
    At this point you can add the nodes (elements, attributes) into the document. First you have to declare the root element node.
    DATA: l_element_root TYPE REF TO if_ixml_element.
    This node we have to give a name and add it (create_simple_node) to the document object l_document, which will be the parent of this node.
    l_element_root  = l_document->create_simple_element(
                name = 'flights'
              parent = l_document ).
    Next we can add child nodes to there parent node using the same method of the document object.
    DATA: l_element_airline TYPE REF TO if_ixml_element,
    l_element_airline  = l_document->create_simple_element(
                   name = 'airline'
                 parent = l_element_root  ).
    An attribute can be add easily using the method set_attribute of the element node.
    l_rc = l_element_airline->set_attribute( name = 'code' value = 'LH401' ).
    Now we have finished the document object. Regretfully it can not be displayed in any form due to the fact that it is a binary object. 
    The next step is to convert the created document to a flat file. To achieve this we have to create a stream factory, which will help us to create an output stream.
    DATA: l_streamfactory   TYPE REF TO if_ixml_stream_factory.
    l_streamfactory = l_ixml->create_stream_factory( ).
    In this case, we will convert the document into an output stream which is based on an internal table of type x.
    TYPES: BEGIN OF xml_line,
              data(256) TYPE x,
            END OF xml_line.
    DATA:  l_xml_table       TYPE TABLE OF xml_line,
            l_xml_size        TYPE i,
            l_rc              TYPE i,
            l_ostream         TYPE REF TO if_ixml_ostream.
    l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table ).
    When we have created the output stream we can do the rendering from the document into the stream. The XML data will be stored in the internal table automatically.
    DATA: l_renderer        TYPE REF TO if_ixml_renderer.
    l_renderer = l_ixml->create_renderer( ostream  = l_ostream
                            & nbsp;              document = l_document ).
    l_rc = l_renderer->render( ).
    In the last step we upload the file to the sapgui
    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.
    This finished the first step-of-three. As mentioned before the next log will focus on the conversion from xml files (back) to abap tables.
    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.
    <?xml version="1.0"?>
    <flights>
         <airline code="AA" name="American Airlines">
              <flight number="0017">
                   <from airport="JFK">NEW YORK,US</from>
                   <to airport="SFO">SAN FRANCISCO,US</to>
                   <departure>110000</departure>
                   <arrival>140100</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0064">
                   <from airport="SFO">SAN FRANCISCO,US</from>
                   <to airport="JFK">NEW YORK,US</to>
                   <departure>090000</departure>
                   <arrival>172100</arrival>
                   <type>Scheduled</type>
              </flight>
         </airline>
         <airline code="AZ" name="Alitalia">
              <flight number="0555">
                   <from airport="FCO">ROME,IT</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>190000</departure>
                   <arrival>210500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0788">
                   <from airport="FCO">ROME,IT</from>
                   <to airport="TYO">TOKYO,JP</to>
                   <departure>120000</departure>
                   <arrival>085500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0789">
                   <from airport="TYO">TOKYO,JP</from>
                   <to airport="FCO">ROME,IT</to>
                   <departure>114500</departure>
                   <arrival>192500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0790">
                   <from airport="FCO">ROME,IT</from>
                   <to airport="KIX">OSAKA,JP</to>
                   <departure>103500</departure>
                   <arrival>081000</arrival>
                   <type>Charter</type>
              </flight>
         </airline>
         <airline code="DL" name="Delta Airlines">
              <flight number="1984">
                   <from airport="SFO">SAN FRANCISCO,US</from>
                   <to airport="JFK">NEW YORK,US</to>
                   <departure>100000</departure>
                   <arrival>182500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="1699">
                   <from airport="JFK">NEW YORK,US</from>
                   <to airport="SFO">SAN FRANCISCO,US</to>
                   <departure>171500</departure>
                   <arrival>203700</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0106">
                   <from airport="JFK">NEW YORK,US</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>193500</departure>
                   <arrival>093000</arrival>
                   <type>Scheduled</type>
              </flight>
         </airline>
         <airline code="JL" name="Japan Airlines">
              <flight number="0407">
                   <from airport="NRT">TOKYO,JP</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>133000</departure>
                   <arrival>173500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0408">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="NRT">TOKYO,JP</to>
                   <departure>202500</departure>
                   <arrival>154000</arrival>
                   <type>Charter</type>
              </flight>
         </airline>
         <airline code="LH" name="Lufthansa">
              <flight number="2407">
                   <from airport="TXL">BERLIN,DE</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>071000</departure>
                   <arrival>081500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="2402">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="SXF">BERLIN,DE</to>
                   <departure>103000</departure>
                   <arrival>113500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0402">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="JFK">NEW YORK,US</to>
                   <departure>133000</departure>
                   <arrival>150500</arrival>
                   <type>Charter</type>
              </flight>
              <flight number="0401">
                   <from airport="JFK">NEW YORK,US</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>183000</departure>
                   <arrival>074500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0400">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="JFK">NEW YORK,US</to>
                   <departure>101000</departure>
                   <arrival>113400</arrival>
                   <type>Scheduled</type>
              </flight>
         </airline>
         <airline code="QF" name="Qantas Airways">
              <flight number="0005">
                   <from airport="SIN">SINGAPORE,SG</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>225000</departure>
                   <arrival>053500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0006">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="SIN">SINGAPORE,SG</to>
                   <departure>205500</departure>
                   <arrival>150500</arrival>
                   <type>Scheduled</type>
              </flight>
         </airline>
         <airline code="SQ" name="Singapore Airlines">
              <flight number="0988">
                   <from airport="SIN">SINGAPORE,SG</from>
                   <to airport="TYO">TOKYO,JP</to>
                   <departure>163500</departure>
                   <arrival>001500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0158">
                   <from airport="SIN">SINGAPORE,SG</from>
                   <to airport="JKT">JAKARTA,ID</to>
                   <departure>152500</departure>
                   <arrival>160000</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0015">
                   <from airport="SFO">SAN FRANCISCO,US</from>
                   <to airport="SIN">SINGAPORE,SG</to>
                   <departure>160000</departure>
                   <arrival>024500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="0002">
                   <from airport="SIN">SINGAPORE,SG</from>
                   <to airport="SFO">SAN FRANCISCO,US</to>
                   <departure>170000</departure>
                   <arrival>192500</arrival>
                   <type>Scheduled</type>
              </flight>
         </airline>
         <airline code="UA" name="United Airlines">
              <flight number="0941">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="SFO">SAN FRANCISCO,US</to>
                   <departure>143000</departure>
                   <arrival>170600</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="3504">
                   <from airport="SFO">SAN FRANCISCO,US</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>150000</departure>
                   <arrival>103000</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="3516">
                   <from airport="JFK">NEW YORK,US</from>
                   <to airport="FRA">FRANKFURT,DE</to>
                   <departure>162000</departure>
                   <arrival>054500</arrival>
                   <type>Scheduled</type>
              </flight>
              <flight number="3517">
                   <from airport="FRA">FRANKFURT,DE</from>
                   <to airport="JFK">NEW YORK,US</to>
                   <departure>104000</departure>
                   <arrival>125500</arrival>
                   <type>Scheduled</type>
              </flight>
         </airline>
    </flights>
    Regards,
    Amit
    Reward all helpful replies.

Maybe you are looking for