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

XML to Internal table

Former Member
0 Likes
983

Hi,

I have checked most of the threads on this topic however was'nt able to figure out how I could implement in my scenario.

I have two XMl files of IDOC type, now i have to write a program to combine both these XML's into one XML file.I have no Knowledge of XSLT and iXML, could anybody help me on this please points would be rewarded for sure...thanks,

7 REPLIES 7
Read only

Former Member
0 Likes
772

Hello John,

i don´t no what your question "XML to Internal table" as in common with "how to merge two IDOCs", but i guess you will merge the to xml documents (idocs) at first before you`ll transform it to an abap internal table.

My proposal is to use the ixml library to merge the two idocs and a xslt-program to transform the xml to abap.

Here some code which will help you to merge the two xml files.

I mentioned that you have the idocs as an xstring ( variable iv_doc_1 and iv_doc_2 ). The source could be also a string.

For further infos look at this help.sap.com link

http://help.sap.com/saphelp_47x200/helpdata/en/47/b5413acdb62f70e10000000a114084/content.htm


DATA: lr_ixml                TYPE REF TO if_ixml,
      lr_ixml_doc_1          TYPE REF TO if_ixml_document,
      lr_ixml_doc_2					 TYPE REF TO if_ixml_document,
      lr_ixml_doc_out				 TYPE REF TO if_ixml_document,
      lr_ixml_stream_factory TYPE REF TO if_ixml_stream_factory,
      lr_ixml_istream        TYPE REF TO if_ixml_istream,
      lr_ixml_parser         TYPE REF TO if_ixml_parser,
      lr_ixml_new_root       type ref to if_ixml_element,
      lr_ixml_root_1				 type ref to if_ixml_element,
      lr_ixml_root_2				 type ref to if_ixml_element,
      lp_value               type string.
 
* create ixml factory
lr_ixml                = cl_ixml=>create( ).

* create ixml documents for idocs and one for output
lr_ixml_doc_1          = lr_ixml->create_document( ).
lr_ixml_doc_2          = lr_ixml->create_document( ).
lr_ixml_doc_out		     = lr_ixml->create_document( ).

* create stream factory
lr_ixml_stream_factory = lr_ixml->create_stream_factory( ).
 
* create input streams for idoc 1 and idoc 2
lr_ixml_istream_1 = lr_ixml_stream_factory->create_istream_xstring( iv_idoc_1 ).
lr_ixml_istream_2 = lr_ixml_stream_factory->create_istream_xstring( iv_idoc_2 ).

* create xml parser to parse idoc 1 
lr_ixml_parser = lr_ixml->create_parser( document       = lr_ixml_doc_1
                                         istream        = lr_ixml_istream_1
                                         stream_factory = lr_ixml_stream_factory ).
 
* parse idoc 1
lr_ixml_parser->parse( ).

* create xml parser to parse idoc 2
lr_ixml_parser = lr_ixml->create_parser( document       = lr_ixml_doc_2
                                         istream        = lr_ixml_istream_2
                                         stream_factory = lr_ixml_stream_factory ).
 
 * parse idoc 2
lr_ixml_parser->parse( ).


* create a root node in the output document
lr_ixml_new_root = lr_ixml_doc_out->create_element( name ='Merged' ).
lr_ixml_doc_out->append_child( lr_ixml_new_root ).

* determine the root nodes of the idocs
lr_ixml_root_1 = lr_ixml_doc_1->get_root_element( ).
lr_ixml_root_2 = lr_ixml_doc_2->get_root_element( ).

* append the root node to the new root node
lr_ixml_new_root->append_child( lr_ixml_root_1 ).
lr_ixml_new_root->append_child( lr_ixml_root_2 ).

So, the two idocs should be merged in lr_ixml_doc_out.

Now, you can write a xslt program which transform the ixml-dom object

into a internal table.



data: lr_result type table of z_my_structure. 

* transform xml source to internal table
CALL TRANSFORMATION 'Z_MYIDOCS_TO_ABAP'
	SOURCE XML lr_ixml_doc_out
	RSULT ITAB = lt_result
.

How to write a XSLT program look a this http://help.sap.com/saphelp_47x200/helpdata/en/fd/9d7348389211d596a200a0c94260a5/content.htm

Also the weblog of Tobias Trapp will help you to transform the xml source to abap with XSLT.

/people/tobias.trapp/blog/2006/10/06/xml-processing-in-abap-part-9-150-abap-processing-using-xslt

Hope it helps

Regards

Sebastian

Read only

athavanraja
Active Contributor
0 Likes
772

check this. is this what you are looking for?

REPORT ycombindxml
       NO STANDARD PAGE HEADING.

DATA: flights TYPE STANDARD TABLE OF sflight ,
      booking TYPE STANDARD TABLE OF bookings .

DATA: f_xml TYPE string ,
      b_xml TYPE string ,
      fb_xml TYPE string .

REFRESH: flights, booking .

SELECT * UP TO 10 ROWS FROM sflight INTO TABLE flights .
SELECT * UP TO 5 ROWS FROM bookings INTO TABLE booking .

CLEAR : f_xml, b_xml, fb_xml .

CALL TRANSFORMATION (`ID`)
         SOURCE flights   = flights[]
         RESULT XML f_xml.

CALL TRANSFORMATION (`ID`)
         SOURCE booking   = booking[]
         RESULT XML b_xml.

CALL TRANSFORMATION (`ID`)
         SOURCE xmlf = f_xml
                xmlb =  b_xml
         RESULT XML fb_xml.

Regards

Raja

Read only

Former Member
0 Likes
772

hey Thx Experts, for all the inputs,

i would try and implement this and hope it works if any problem i shall get back for ur help, thanks a lot again.

Read only

Former Member
0 Likes
772

Hey Experts,

I am trying to fetch the two XMl files from the App Server, So i would be using OPEN DATASET,

So how would i Be assigning the two strings Str1 (type:Xstring)and Str2 (type:Xstring) to lr_ixml_doc_1 & lr_ixml_doc_2.

any thing i need to specially consider here...? please help..!!!

Read only

0 Likes
772

Hello Andrew,

replace in the following lines iv_idoc_1 by Str1 and iv_idoc_2 by Str2.


lr_ixml_istream_1 = lr_ixml_stream_factory->create_istream_xstring( iv_idoc_1 ).
lr_ixml_istream_2 = lr_ixml_stream_factory->create_istream_xstring( iv_idoc_2 ).

Regards

Sebastian

Read only

Former Member
0 Likes
772

thanks Sebastin,

STR1 and STR2 are of STRING not XSTRING type, so i have changed the following code, is it correct

lr_ixml_istream_1 = lr_ixml_stream_factory->create_istream_string( STR1).

lr_ixml_istream_2 = lr_ixml_stream_factory->create_istream_string( STR2).

i have changed "create_istream_xstring" to "create_istream_string" i hope thats the way we do it.

and is there anything else i need to change....?

It did not give me an error though, need your suggestions on this. thanks for all ur help

Read only

0 Likes
772

hello andrew,

thats all you have to chance. to see or test the merged xml docs append the following lines to your report.

data: lr_ixml_ostream type ref to if_ixml_ostream,

lr_ixml_stream_factory TYPE REF TO if_ixml_stream_factory,

lr_ixml_renderer type ref to if_ixml_renderer,

lp_xml_out type string.

lr_ixml_ostream = lr_ixml_stream_factory->create_ostream_cstring( importing string = lp_xml_out ).

lr_ixml_renderer = lr_ixml->create_renderer( ostream = lr_ixml_ostream

document = lr_ixml_doc_out ).

lr_ixml_renderer->render( ).

write lp_xml_out.