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

Function module to read XML file in ABAP

Former Member
0 Likes
2,421

Hey guys

is there any function module which can read an XML file into an ABAP code?

we are getting some file on the application server in XML format and we need to read this file in ABAP code,how can i achieve this ?

thanz

ahmad

3 REPLIES 3
Read only

ferry_lianto
Active Contributor
0 Likes
1,217

Hi,

You can use CALL TRANSFORMATION.

Please check Amit's sample code.

1. itab --- > xml

xml ---> itab.

2. This program will do both.

(just copy paste in new program)

3.

REPORT abc.

*----


DATA

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

DATA : BEGIN OF itab OCCURS 0,

a(100) TYPE c,

END OF itab.

DATA: xml_out TYPE string .

DATA : BEGIN OF upl OCCURS 0,

f(255) TYPE c,

END OF upl.

DATA: xmlupl TYPE string .

                                                              • FIRST PHASE

                                                              • FIRST PHASE

                                                              • FIRST PHASE

*----


Fetch Data

SELECT * FROM t001 INTO TABLE t001.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE tab = t001[]

RESULT XML xml_out.

*----


Convert to TABLE

CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'

EXPORTING

i_string = xml_out

i_tabline_length = 100

TABLES

et_table = itab.

*----


Download

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filetype = 'BIN'

filename = 'd:\xx.xml'

TABLES

data_tab = itab.

                                                              • SECOND PHASE

                                                              • SECOND PHASE

                                                              • SECOND PHASE

BREAK-POINT.

REFRESH t001.

CLEAR t001.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = 'D:\XX.XML'

filetype = 'BIN'

TABLES

data_tab = upl.

LOOP AT upl.

CONCATENATE xmlupl upl-f INTO xmlupl.

ENDLOOP.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE XML xmlupl

RESULT tab = t001[]

.

BREAK-POINT.

Also please check this excellent series of weblogs on transformation.

Introduction:

/people/horst.keller/blog/2004/10/27/abap-and-xml--introducing-st

Details:

/people/tobias.trapp/blog/2005/05/04/xml-processing-in-abap-part-1

/people/tobias.trapp/blog/2005/05/17/xml-processing-in-abap-part-2

/people/tobias.trapp/blog/2005/05/20/xml-processing-in-abap-part-3

/people/tobias.trapp/blog/2005/05/27/xml-processing-in-abap-part-4

/people/tobias.trapp/blog/2005/06/15/xml-processing-in-abap--part-5

/people/tobias.trapp/blog/2005/12/08/xml-processing-in-abap--part-6

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
1,217

Hi Ahmad,

These are few functions to work with XML in ABAP.

SDIXML_DATA_TO_DOM: Convert SAP data (elementary/structured/table types) into DOM (XML)

SDIXML_DOM_TO_XML: Convert DOM (XML) into string of bytes that can be downloaded to PC or application server

SDIXML_DOM_TO_SCREEN: Display DOM (XML)

SDIXML_DOM_TO_DATA

Example of using these:

eport z.

data: it_table like t001 occurs 0.

data: l_dom TYPE REF TO IF_IXML_ELEMENT,

m_document TYPE REF TO IF_IXML_DOCUMENT,

g_ixml TYPE REF TO IF_IXML,

w_string TYPE XSTRING,

w_size TYPE I,

w_result TYPE I,

w_line TYPE STRING,

it_xml TYPE DCXMLLINES,

s_xml like line of it_xml,

w_rc like sy-subrc.

start-of-selection.

select * from t001 into table it_table.

end-of-selection.

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

        • initialize iXML-Framework ****

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

write: / 'initialiazing iXML:'.

class cl_ixml definition load.

g_ixml = cl_ixml=>create( ).

check not g_ixml is initial.

write: 'ok'.

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

        • create DOM from SAP data ****

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

write: / 'creating iXML doc:'.

m_document = g_ixml->create_document( ).

check not m_document is initial.

write: 'ok'.

write: / 'converting DATA TO DOM 1:'.

CALL FUNCTION 'SDIXML_DATA_TO_DOM'

EXPORTING

NAME = 'IT_TABLE'

DATAOBJECT = it_table[]

IMPORTING

DATA_AS_DOM = l_dom

CHANGING

DOCUMENT = m_document

EXCEPTIONS

ILLEGAL_NAME = 1

OTHERS = 2.

if sy-subrc = 0. write 'ok'.

else. write: 'Err =', sy-subrc.

endif.

check not l_dom is initial.

write: / 'appending DOM to iXML doc:'.

w_rc = m_document->append_child( new_child = l_dom ).

if w_rc is initial. write 'ok'.

else. write: 'Err =', w_rc.

endif.

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

        • visualize iXML (DOM) ****

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

write: / 'displaying DOM:'.

CALL FUNCTION 'SDIXML_DOM_TO_SCREEN'

EXPORTING

DOCUMENT = m_document

EXCEPTIONS

NO_DOCUMENT = 1

OTHERS = 2.

if sy-subrc = 0. write 'ok'.

else. write: 'Err =', sy-subrc.

endif.

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

        • convert DOM to XML doc (table) ****

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

write: / 'converting DOM TO XML:'.

CALL FUNCTION 'SDIXML_DOM_TO_XML'

EXPORTING

DOCUMENT = m_document

  • PRETTY_PRINT = ' '

IMPORTING

XML_AS_STRING = w_string

SIZE = w_size

TABLES

XML_AS_TABLE = it_xml

EXCEPTIONS

NO_DOCUMENT = 1

OTHERS = 2.

if sy-subrc = 0. write 'ok'.

else. write: 'Err =', sy-subrc.

endif.

write: / 'XML as string of size:', w_size, / w_string.

describe table it_xml lines w_result.

write: / 'XML as table of', w_result, 'lines:'..

loop at it_xml into s_xml.

write s_xml.

endloop.

write: / 'end of processing'.

  • end of code

Let me know if you have further questions..

BR

Rakesh

Read only

0 Likes
1,217

Thznx guys

actually i m not an ABAper i am an SAP XI consultant and we were having some problems in posting file in CSV format,so we were thinking if somehow we can develop an ABAP code which will read the XML directly instead of reading a CSV file,then it might solve the purpose.

will get back to u guys if we run into any more troubles

thanx

Ahmad