Application Development 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: 

mapping corresponding field in the internal table

Former Member
0 Kudos
639

Hi guys I need help with putting XML value into the appropriate fields in the internal table struc. I attempted to use if else condition to map the element name (cname)over to the fields but those internal table within an internal table post an issue of when to append and so on ... I am using FM SMUM_XML_PARSE and the output in to this table type smum_xmltb (containing all the XML Element and Values) I want to map it to its corresponding field in the internal table (defined below)

Pls pardon this newbie here as this is very new to me. Hope to hear fr u all soon and points will be given! ")

XML->>

<?xml version="1.0" encoding="iso-8859-1"?>

<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">

<asx:values>

<BOOKLIST>

<DATE>2006-09-25</DATE>

<TIME>16:27:20</TIME>

<BOOK_RECORD>

<BOOK_NUM>0012345678</BOOK_NUM>

<SHORT_DESC>OMS BOOK</SHORT_DESC>

<BOOK_GROUP>

<BOOK_CATEGORY>

<CATEGORY_ID>03</CATEGORY_ID>

<CATEGORY_DESC>BOOK group 3</CATEGORY_DESC>

</BOOK_CATEGORY>

<BOOK_CATEGORY>

<CATEGORY_ID>02</CATEGORY_ID>

<CATEGORY_DESC>BOOK group 2</CATEGORY_DESC>

</BOOK_CATEGORY>

</BOOK_GROUP>

</BOOK_RECORD>

<BOOK_RECORD>

<BOOK_NUM>0012345679</BOOK_NUM>

<SHORT_DESC>SAP BOOK</SHORT_DESC>

<BOOK_GROUP>

<BOOK_CATEGORY>

<CATEGORY_ID>01</CATEGORY_ID>

<CATEGORY_DESC>BOOK group 1</CATEGORY_DESC>

</BOOK_CATEGORY>

<BOOK_CATEGORY>

<CATEGORY_ID>09</CATEGORY_ID>

<CATEGORY_DESC>BOOK group 9</CATEGORY_DESC>

</BOOK_CATEGORY>

</BOOK_GROUP>

</BOOK_RECORD>

</BOOKLIST>

</asx:values>

</asx:abap>

INTERNAL TABLE ->>

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-TIMLO,

BOOK_RECORD TYPE ADD_T_BOOK,

END OF TYPE_DATA.

DATA: I_DATA TYPE TYPE_DATA OCCURS 0 WITH HEADER LINE.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
195

Hi Chee,

The way I chose to deal this situation was to break my data-gathering step into forms, one for each level ( of depth) and pass the actual nested field of the table as a parameter ie PERFORM xxx USING wa_data-table[].

The point is that you can use the call stack of all these performs to do half the logic for you.

E.g. A store can have a list of books,

books have a list of pages,

pages can have a list of words, etc....


DATA Structures

  word type char255
  t_word type table of word.

  begin of s_page
    pageID
    content type t_word
  end of s_page

  t_page type table of s_page.

  begin of s_book
    bookId
    pages type t_page.   
  end of s_book

  t_book type table of s_book

  begin of store
    storeId
    books type t_book
  end of store

  wa_store type store.
  t_store type table of store.

** The flat data source **
begin of i_data.
  storeId
  bookId
  pageId
  wordId
end of i_data.

**Processing psuedo code***

loop at list_of_stores into wa_store-storeID
<<when the storeId changes>>
perform get_books using storeId
               changing wa_store-books[]
append wa_store to t_store

<<end loop>>
<<end of start-of-selection or something>>

form get_books using storeId type storeID
            changing it_books type t_book
data: wa_book type s_book.

<<Get books from source based on storeID>>
<<When the bookID changes>>
  perform get_pages using bookId
                 changing wa_book-pages[]
  append wa_book to it_books. 
endform.

form get_pages using bookID type bookID
            changing it_pages type t_page
data: wa_page type s_page.

<<Get pages from source based on bookId>>
<<When the bookID changes>>
  perform get_words using pageId
                 changing wa_page-words[]
  append wa_page to it_pages. 
endform.

So a store record does not get appended until it knows all its books. Similarly a book record does not get appended until we know all its pages.

The logic of using LOCAL WORK AREAS and PASSING THE ACTUAL TABLE to the form means you should have to worry about clearing fields and such.

I hope this applies to your case.

Cheers,

Phil

4 REPLIES 4

anversha_s
Active Contributor
0 Kudos
195

hi,

here is the code sample using ixml library .

just create a report program and copy paste the following code.

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 ,

l_xml TYPE REF TO cl_xml_document .

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' ).

LOOP AT i_data INTO bk_wa .

CLEAR temp_string .

MOVE: bk_wa-date TO temp_string .

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.

************************

to read the xml data to abap itab you could parse node by node or write a XSLT to map it to your itab or use the following method. (add the following code to the earlier program)

data: result_xml type standard table of smum_xmltb .

data: return type standard table of bapiret2 .

after the statement

l_document->render( ostream = l_ostream ).

add

  • converting xml to itab

call function 'SMUM_XML_PARSE'

exporting

xml_input = xml

tables

xml_table = result_xml

return = return .

now check the result_xml itab.

rgds

anver

0 Kudos
195

Hi anver

This was my posting previously and the solution was by Raja. thanks anyways!

To make my qn clearer I got the results in this result_xml itab BUT I need to map this results into I_DATA (which contains internal tables within internal tables) that makes it difficult to map as I have problems (using IF ELSE conditions) with appending the internal tables to the internal tables (look @ the struc of I_DATA & u all will understand).

Anyone else?

Former Member
0 Kudos
196

Hi Chee,

The way I chose to deal this situation was to break my data-gathering step into forms, one for each level ( of depth) and pass the actual nested field of the table as a parameter ie PERFORM xxx USING wa_data-table[].

The point is that you can use the call stack of all these performs to do half the logic for you.

E.g. A store can have a list of books,

books have a list of pages,

pages can have a list of words, etc....


DATA Structures

  word type char255
  t_word type table of word.

  begin of s_page
    pageID
    content type t_word
  end of s_page

  t_page type table of s_page.

  begin of s_book
    bookId
    pages type t_page.   
  end of s_book

  t_book type table of s_book

  begin of store
    storeId
    books type t_book
  end of store

  wa_store type store.
  t_store type table of store.

** The flat data source **
begin of i_data.
  storeId
  bookId
  pageId
  wordId
end of i_data.

**Processing psuedo code***

loop at list_of_stores into wa_store-storeID
<<when the storeId changes>>
perform get_books using storeId
               changing wa_store-books[]
append wa_store to t_store

<<end loop>>
<<end of start-of-selection or something>>

form get_books using storeId type storeID
            changing it_books type t_book
data: wa_book type s_book.

<<Get books from source based on storeID>>
<<When the bookID changes>>
  perform get_pages using bookId
                 changing wa_book-pages[]
  append wa_book to it_books. 
endform.

form get_pages using bookID type bookID
            changing it_pages type t_page
data: wa_page type s_page.

<<Get pages from source based on bookId>>
<<When the bookID changes>>
  perform get_words using pageId
                 changing wa_page-words[]
  append wa_page to it_pages. 
endform.

So a store record does not get appended until it knows all its books. Similarly a book record does not get appended until we know all its pages.

The logic of using LOCAL WORK AREAS and PASSING THE ACTUAL TABLE to the form means you should have to worry about clearing fields and such.

I hope this applies to your case.

Cheers,

Phil

0 Kudos
195

Fantastic! It works! Thanks Dude!

While waiting for an answer I manage to solve it using Call Transformation via XSLT. Taking in XML string as a source and output to the struc i defined.

Cheers!