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

Read data from xml files and populate internal table

Former Member
0 Likes
9,165

Hi.

How to read data from xml files into internal tables?

Can u tell me the classes and methods to read xml data..

Can u explain it with a sample program...

Hi.

How to read data from xml files into internal tables?

Can u tell me the classes and methods to read xml data..

Can u explain it with a sample program...

8 REPLIES 8
Read only

Former Member
0 Likes
3,721

plz chk this link:

/people/r.eijpe/blog/2005/11/21/xml-dom-processing-in-abap-part-ii--convert-an-xml-file-into-an-abap-table-using-sap-dom-approach

Read only

GauthamV
Active Contributor
0 Likes
3,721

Use this function module to populate data from xml to internal table.

SMUM_XML_PARSE

Read only

Former Member
0 Likes
3,721

hi,

do like this it works..just copy paste and execute...

TYPES: BEGIN OF ty_xml_line,

data(256) TYPE x,

END OF ty_xml_line.

data : gt_file type STANDARD TABLE OF SMUM_XMLTB,

gt_return type STANDARD TABLE OF bapiret2,

gt_xml_table type STANDARD TABLE OF ty_xml_line,

gs_xml_table type ty_xml_line,

gs_xml_table1 type xstring,

gv_xml_table_size type i,

gv_xml type string.

DATA: gv_filename TYPE string.

SELECTION-SCREEN BEGIN OF BLOCK un WITH FRAME TITLE text-003.

PARAMETERS : un_xmlf LIKE rlgrap-filename MODIF ID us.

SELECTION-SCREEN END OF BLOCK un.

PARAMETERS: un_fnam LIKE rlgrap-filename NO-DISPLAY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR un_xmlf.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

file_name = un_xmlf.

START-OF-SELECTION.

gv_filename = un_xmlf.

CALL METHOD cl_gui_frontend_services=>gui_upload

EXPORTING

filename = gv_filename

filetype = 'BIN'

codepage = '16385'

IMPORTING

filelength = gv_xml_table_size

CHANGING

data_tab = gt_xml_table

EXCEPTIONS

OTHERS = 19.

IF sy-subrc <> 0.

MESSAGE i000 DISPLAY LIKE 'E' WITH text-040.

LEAVE LIST-PROCESSING.

ENDIF.

  • Here u will have all the xml data in the form of XSTRING but this is in table format. in order to pass this data in to below function module , we have to loop at the table and concatenate in to one single field.

data : lv_xml type string.

loop at gt_xml_table into gs_xml_table.

lv_xml = gs_xml_table.

concatenate gv_xml lv_xml into gv_xml. // concatenating the values

endloop.

clear gs_xml_table1.

gs_xml_table1 = gv_xml. // pass the string to XString datatype and tehn pass to FM

CALL FUNCTION 'SMUM_XML_PARSE'

EXPORTING

xml_input = gs_xml_table1

tables

xml_table = gt_file

return = gt_return

.

write 'hello'. // here check the data in gt_file table

Rgds.,

subash

Read only

0 Likes
3,721

TYPES: BEGIN OF ty_xml_line,

data(256) TYPE x,

END OF ty_xml_line.

data : gt_file type STANDARD TABLE OF SMUM_XMLTB,

gt_return type STANDARD TABLE OF bapiret2,

gt_xml_table type STANDARD TABLE OF ty_xml_line,

gs_xml_table type ty_xml_line,

gs_xml_table1 type xstring,

gv_xml_table_size type i,

gv_xml type string.

DATA: gv_filename TYPE string.

SELECTION-SCREEN BEGIN OF BLOCK un WITH FRAME TITLE text-003.

PARAMETERS : un_xmlf LIKE rlgrap-filename MODIF ID us.

SELECTION-SCREEN END OF BLOCK un.

PARAMETERS: un_fnam LIKE rlgrap-filename NO-DISPLAY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR un_xmlf.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

file_name = un_xmlf.

START-OF-SELECTION.

gv_filename = un_xmlf.

CALL METHOD cl_gui_frontend_services=>gui_upload

EXPORTING

filename = gv_filename

filetype = 'BIN'

codepage = '16385'

IMPORTING

filelength = gv_xml_table_size

CHANGING

data_tab = gt_xml_table

EXCEPTIONS

OTHERS = 19.

IF sy-subrc 0.

MESSAGE i000 DISPLAY LIKE 'E' WITH text-040.

LEAVE LIST-PROCESSING.

ENDIF.

  • Here u will have all the xml data in the form of XSTRING but this is in table format. in order to pass this data in to below function module , we have to loop at the table and concatenate in to one single field.

data : lv_xml type string.

loop at gt_xml_table into gs_xml_table.

lv_xml = gs_xml_table. // small correction lv_xml = gs_xml_table-data

concatenate gv_xml lv_xml into gv_xml. // concatenating the values

endloop.

clear gs_xml_table1.

gs_xml_table1 = gv_xml. // pass the string to XString datatype and tehn pass to FM

CALL FUNCTION 'SMUM_XML_PARSE'

EXPORTING

xml_input = gs_xml_table1

tables

xml_table = gt_file

return = gt_return

.

write 'hello'. // here check the data in gt_file table

Rgds.,

subash

Read only

Former Member
0 Likes
3,721

<pre>DATA itab_accontextdir TYPE TABLE OF ACCONTEXTDIR.

DATA struct_accontextdir LIKE LINE OF itab_accontextdir.

DATA l_o_error TYPE REF TO cx_root.

DATA: filename type string ,

xmldata type xstring .

DATA: mr TYPE REF TO if_mr_api.

mr = cl_mime_repository_api=>get_api( ).

mr->get( EXPORTING i_url = 'SAP/PUBLIC/BC/xml_files_accontext/xml_accontextdir.xml'

IMPORTING e_content = xmldata ).

WRITE xmldata.

TRY.

CALL TRANSFORMATION id

SOURCE XML xmldata

RESULT shiva = itab_accontextdir.

CATCH cx_root INTO l_o_error.

ENDTRY.

LOOP AT itab_accontextdir INTO struct_accontextdir.

WRITE: / struct_accontextdir-context_id,

struct_accontextdir-context_name,

struct_accontextdir-context_type.

NEW-LINE.

ENDLOOP.</pre>

<br/>

Description:

In the above code snippet I am storing the data in an xml file(you know xml is used to store and transport data ) called 'xml_accontextdir.xml' that is uploaded into the MIME repository at path 'SAP/PUBLIC/BC/xml_files_accontext/xml_accontextdir.xml'.

The below API is used to read a file in MIME repo and convert it into a string that is stored in ' xmldata'. (This is just a raw data that is got by appending the each line of xml file).

mr = cl_mime_repository_api=>get_api( ).

mr->get( EXPORTING i_url = 'SAP/PUBLIC/BC/xml_files_accontext/xml_accontextdir.xml'

IMPORTING e_content = xmldata ).

Once the 'xmldata' string is available we use the tranformation to parse the xml string that we have got from the above API and convert it into the internal table.

<pre>TRY.

CALL TRANSFORMATION id

SOURCE XML xmldata

RESULT shiva = itab_accontextdir.

CATCH cx_root INTO l_o_error.

ENDTRY.</pre>

Here the trasnsformation 'id ' is used to conververt the source xml 'xmldata' to resulting internal table itab_accontextdir, that have same structure as our xml file 'xml_accontextdir.xml'. In the RESULT root of the xml file has to be specified. (In my the root is 'shiva').

Things to be taken care:

One of the major problem that occurs when reading the xml file is 'format not compatible with the internal table' that you are reading into internal table. Iin order to get rid of this issue use one more tranformation to convert the data from the internal table into the xml file.

<pre>TRY.

CALL TRANSFORMATION id

SOURCE shiv = t_internal_tab

RESULT XML xml.

CATCH cx_root INTO l_o_error.

ENDTRY.

WRITE xml.

NEW-LINE.</pre>

<br/>

This is the same transformation that we used above but the differnce is that the SOURCE and RESULT parameters are changed the source is now the internal table and result is *xml *string. Use xml browser that is available with the ABAP workbench to read the xml string displayed with proper indentation. In this way we get the format of xml file to be used that is compatable with the given internal table.

Thank you, Hope this will help you!!!

Edited by: Shiva Prasad L on Jun 15, 2009 7:30 AM

Edited by: Shiva Prasad L on Jun 15, 2009 11:56 AM

Edited by: Shiva Prasad L on Jun 15, 2009 12:06 PM

Read only

Former Member
0 Likes
3,721

Try to use following FM

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

EXPORTING

filename = filename

i_begin_col = begcol

i_begin_row = begrow

i_end_col = endcol

i_end_row = endrow

TABLES

intern = intern

EXCEPTIONS

inconsistent_parameters = 1

upload_ole = 2

OTHERS = 3.

IF sy-subrc <> 0.

WRITE:/ 'Upload Error ', sy-subrc.

ENDIF.

Read only

Former Member
0 Likes
3,721

Hi Ramana,

I think transformations can only be used in case of Tabular formats. Use the function module SMUM_XML_PARSE to get the contents of a xml file into an Internal Table. The input has to be the xstring format. Once you can get the resultant xml_table you can differentiate b/w the tags and the actual data.

Thanks,

Sai

Read only

Former Member
0 Likes
3,721

go through the following links...it will defientely solve the problem..

<pre>

https://wiki.sdn.sap.com/wiki/display/Snippets/SimulatortoreaddatafromaxmlfilestoredintheMIMErepositoryandconvertitintotheinternal+table.

</pre>