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

internal table to xml with structured hierarchy

Former Member
0 Likes
599

Hi All,

I have an internal table itab1 which is a flat structure.Lets say it has fields

-First Name

-Last Name

-Address Line 1

-Address Line 2

-Postcode

I also have another internal table itab2 in which i define the following 2 fields

Parent Child

Name First Name

NAme Last Name

Address Address Line 1

Address Address Line 2

Address Post Code.

I want to convert the entries in itab1 to xml. However this xml should be structured and should have the children tags within the parent tags.

So it should be of the form

<Name>

<First Name>Preethi</First Name>

<Last Name>P</LastName>

</Name>

the same for parent tag <Address>.

Nothing should really be hardocded. So everytime i update the table 2 or table 1 my code should automatically generate the right xml.

Any clues?? Has anyone done this before.

I have so far successfully generated the xml in a flat file structure for table one. How do i parse it and insert these parent tags and alsign children accordingly?

Is this the right approach??

Regards,

Preethi

*..10 for anyone who is really helpful...

Hi All,

I have an internal table itab1 which is a flat structure.Lets say it has fields

-First Name

-Last Name

-Address Line 1

-Address Line 2

-Postcode

I also have another internal table itab2 in which i define the following 2 fields

Parent Child

Name First Name

NAme Last Name

Address Address Line 1

Address Address Line 2

Address Post Code.

I want to convert the entries in itab1 to xml. However this xml should be structured and should have the children tags within the parent tags.

So it should be of the form

<Name>

<First Name>Preethi</First Name>

<Last Name>P</LastName>

</Name>

the same for parent tag <Address>.

Nothing should really be hardocded. So everytime i update the table 2 or table 1 my code should automatically generate the right xml.

Any clues?? Has anyone done this before.

I have so far successfully generated the xml in a flat file structure for table one. How do i parse it and insert these parent tags and alsign children accordingly?

Is this the right approach??

Regards,

Preethi

*..10 for anyone who is really helpful...

1 REPLY 1
Read only

athavanraja
Active Contributor
0 Likes
480

all you need is a properly built nested itab. check this sample.

REPORT ytestxmlnesteditab
       NO STANDARD PAGE HEADING.

TYPES: BEGIN OF name_ty ,
       firstname(100) ,
       lastname(100) ,
       END OF name_ty .

TYPES: BEGIN OF addr_ty ,
       addr1(100),
       addr2(100) ,
       END OF addr_ty .

TYPES: name1 TYPE name_ty OCCURS 0 ,
       address1 TYPE addr_ty OCCURS 0 .

TYPES: BEGIN OF out_ty ,
       name TYPE name1 ,
       address TYPE address1 ,
       END OF out_ty .

DATA: output TYPE STANDARD TABLE OF out_ty .
DATA: nametab TYPE name1 ,
      addrtab TYPE address1 .

DATA: wa_out TYPE out_ty .
data: xslt_error	type ref to	cx_xslt_exception,
xslt_message	type	string ,
xml_out type string .

DATA: name_wa TYPE name_ty ,
      addr_wa TYPE addr_ty .

CLEAR : name_wa, wa_out, addr_wa .

MOVE: 'one first' TO name_wa-firstname ,
      'one last' TO name_wa-lastname .
APPEND name_wa TO nametab .

CLEAR addr_wa .
MOVE: 'one addr first' TO addr_wa-addr1 ,
      'one addr last' TO addr_wa-addr2 .

APPEND addr_wa TO addrtab  .

wa_out-name[] = nametab[] .
wa_out-address[] = addrtab[] .
APPEND wa_out TO output .

clear wa_out .

CLEAR : name_wa, wa_out, addr_wa , nametab, addrtab .

MOVE: 'two first' TO name_wa-firstname ,
      'two last' TO name_wa-lastname .
APPEND name_wa TO nametab .

CLEAR addr_wa .
MOVE: 'two addr first' TO addr_wa-addr1 ,
      'two addr last' TO addr_wa-addr2 .

APPEND addr_wa TO addrtab  .

wa_out-name[] = nametab[] .
wa_out-address[] = addrtab[] .
APPEND wa_out TO output .

try .
    call transformation (`ID`)
    source output = output[]
    result xml xml_out.
  catch cx_xslt_exception into xslt_error.
    xslt_message = xslt_error->get_text( ).
endtry.

of check out this alternate method.

*&---------------------------------------------------------------------*
*& Report  Y_TEST_XML
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  y_test_xml.
data: l_ixml                                 type ref to if_ixml,
        l_ixml_sf                              type ref to if_ixml_stream_factory,
        l_istream                              type ref to if_ixml_istream,
        l_ostream                              type ref to if_ixml_ostream,
        l_booklist                   type ref to if_ixml_element,
        l_document                             type ref to if_ixml_document,
        l_parser                               type ref to if_ixml_parser,
        l_root_element                         type ref to if_ixml_element,
        l_book_record                            type ref to if_ixml_element,
        l_date type ref to if_ixml_element ,
                l_time type ref to if_ixml_element ,

        l_book_group                            type ref to if_ixml_element,
        l_book_cat                            type ref to if_ixml_element ,
        others                                  type ref to if_ixml_element ,
        link                                    type ref to if_ixml_element ,
        description                            type ref to if_ixml_element ,
        xml                                    type xstring ,
        size type  i ,
        lp_cdata    type ref to if_ixml_cdata_section ,
         l_xml  type ref to cl_xml_document  .

data: result_xml type standard table of smum_xmltb .
data: return type standard table of bapiret2 .


data: xml_out type string ,
      temp_string type string .

types: begin of add_category,
category_id(10),
category_desc(40),
end of add_category,
add_t_category type add_category occurs 0 .

types: begin of add_book_grp,
book_category type add_t_category,
end of add_book_grp,
add_t_book_grp type add_book_grp occurs 0.

types: begin of add_book,
book_num(10) type c,
short_desc(40) type c,
book_group type add_t_book_grp,
end of add_book,
add_t_book type add_book occurs 0.

types: begin of type_data,
date type sy-datum,
time type sy-uzeit,
book_record type add_t_book,
end of type_data.
data: i_data type type_data occurs 0 with header line.

data: itab like soli occurs 0 with header line.

data: cat_wa type add_category ,
      bk_gp_wa type add_book_grp ,
      bk_rec_wa type add_book ,
      bk_wa like line of i_data .

data: cat_tab type standard table of add_category ,
      bk_gp_tab type standard table of add_book_grp ,
      bk_rec_tab type standard table of add_book .


move: '03' to cat_wa-category_id  ,
      ' BK GP 3' to cat_wa-category_desc .
append cat_wa to cat_tab .
move: '02' to cat_wa-category_id  ,
      ' BK GP 2' to cat_wa-category_desc .
append cat_wa to cat_tab .
bk_gp_wa-book_category  = cat_tab.
append bk_gp_wa to bk_gp_tab .
*
move: '0012345678' to bk_rec_wa-book_num ,
      'OMS book' to bk_rec_wa-short_desc .
bk_rec_wa-book_group = bk_gp_tab .
append bk_rec_wa to bk_rec_tab .
clear:bk_gp_tab, cat_tab .
refresh :bk_gp_tab, cat_tab .

move: '01' to cat_wa-category_id  ,
      ' BK GP 1' to cat_wa-category_desc .
append cat_wa to cat_tab .
move: '09' to cat_wa-category_id  ,
      ' BK GP 9' to cat_wa-category_desc .
append cat_wa to cat_tab .
bk_gp_wa-book_category  = cat_tab.
append bk_gp_wa to bk_gp_tab .

move: '00123456789' to bk_rec_wa-book_num ,
      'SAP book' to bk_rec_wa-short_desc .
bk_rec_wa-book_group = bk_gp_tab .
append bk_rec_wa to bk_rec_tab .

move: sy-datum to bk_wa-date ,
      sy-uzeit to bk_wa-time .
bk_wa-book_record = bk_rec_tab .
append bk_wa to i_data .

clear: cat_wa , bk_gp_wa ,bk_rec_wa , bk_wa .
l_ixml = cl_ixml=>create( ).
l_ixml_sf = l_ixml->create_stream_factory( ).
l_document = l_ixml->create_document( ).

l_root_element = l_document->create_element( name = 'asx:abap' ).
l_root_element->set_attribute( name = 'xmlns:asx' value = 'http://www.sap.com/abapxml' ) .
l_root_element->set_attribute( name = 'version' value = '1.0' ).
l_document->append_child( new_child = l_root_element ).
others = l_document->create_simple_element( parent = l_root_element name = 'asx:values' ).
l_booklist = l_document->create_simple_element( parent = others name = 'BOOKLIST' ).
           call method l_document->create_cdata_section
             exporting
               cdata  = 'This is for testing'
             receiving
               rval   = lp_cdata .
loop at i_data into bk_wa .
  clear temp_string .
  move: bk_wa-date to temp_string .
  clear temp_string .
concatenate   `<![CDATA[`
`First Line of Text`
`Second line of Text`
`Third Line of Text`
`` into temp_string separated by cl_abap_char_utilities=>newline.

  l_date = l_document->create_simple_element( parent = l_booklist name = 'DATE' value = temp_string  ).
  clear temp_string .
  move: bk_wa-time to temp_string .

  l_time = l_document->create_simple_element( parent = l_booklist name = 'TIME' value = temp_string ).

  loop at bk_wa-book_record into bk_rec_wa .
    l_book_record = l_document->create_simple_element( parent = l_booklist name = 'BOOK_RECORD' ) .
    clear temp_string .
    move: bk_rec_wa-book_num to temp_string .

    l_date = l_document->create_simple_element( parent = l_book_record name = 'BOOK_NUM' value = temp_string ).
    clear temp_string .
    move: bk_rec_wa-short_desc to temp_string .

    l_time = l_document->create_simple_element( parent = l_book_record name = 'SHORT_DESC' value = temp_string ).
    l_book_group = l_document->create_simple_element( parent = l_book_record name = 'BOOK_GROUP' ).
    loop at bk_rec_wa-book_group into bk_gp_wa .
      loop at bk_gp_wa-book_category into cat_wa .
        l_book_cat = l_document->create_simple_element( parent = l_book_group name = 'BOOK_CATEGORY' ).

        clear temp_string .
        move: cat_wa-category_id to temp_string .

        l_date = l_document->create_simple_element( parent = l_book_cat name = 'CATEGORY_ID' value = temp_string ).
        clear temp_string .
        move: cat_wa-category_desc to temp_string .

        l_time = l_document->create_simple_element( parent = l_book_cat name = 'CATEGORY_DESC' value = temp_string ).

      endloop .

    endloop .
  endloop .

endloop .


l_ostream = l_ixml_sf->create_ostream_xstring( xml ).
l_document->render( ostream = l_ostream ).

create object l_xml.
call method l_xml->parse_xstring
  exporting
    stream = xml.
l_xml->render_2_string(
  exporting
    pretty_print = 'X'
  importing
*    RETCODE      = RETCODE
    stream       = xml_out
    size         = size
       ).

call method l_xml->display.
]]>

Raja