Use function module SMUM_XML_PARSE to parse XML document into a table structure.
Let us upload the below XML document that contains student information to student internal table.
Below code uploads the XML file to XML table using FM SMUM_XML_PARSE.
*&---------------------------------------------------------------------* *& Data Declaration *&---------------------------------------------------------------------* DATA: gcl_xml TYPE REF TO cl_xml_document. DATA: gv_subrc TYPE sy-subrc. DATA: gv_xml_string TYPE xstring. DATA: gv_size TYPE sytabix. DATA: gt_xml_data TYPE TABLE OF smum_xmltb. DATA: gwa_xml_data TYPE smum_xmltb. DATA: gt_return TYPE TABLE OF bapiret2. DATA: gv_tabix TYPE sytabix. *&---------------------------------------------------------------------* *& start-of-selection *&---------------------------------------------------------------------* START-OF-SELECTION. CREATE OBJECT gcl_xml. *Upload XML File CALL METHOD gcl_xml->import_from_file EXPORTING filename = 'c:\test.xml' RECEIVING retcode = gv_subrc. IF gv_subrc = 0. CALL METHOD gcl_xml->render_2_xstring IMPORTING retcode = gv_subrc stream = gv_xml_string size = gv_size. IF gv_subrc = 0. * Convert XML to internal table CALL FUNCTION 'SMUM_XML_PARSE' EXPORTING xml_input = gv_xml_string TABLES xml_table = gt_xml_data return = gt_return. ENDIF. ENDIF. WRITE:/ 'Heirarchy',11 'Type',17 'Name',37 'Value'. LOOP AT gt_xml_data INTO gwa_xml_data. WRITE:/ gwa_xml_data-hier,11 gwa_xml_data-type,17 gwa_xml_data-cname,37 gwa_xml_data-cvalue. CLEAR gwa_xml_data. ENDLOOP.
Output
In the above XML file, List is the root element. So the the hierarchy for List is 1. Student is child of List and hence the hierarchy of Student is 2 and so on.
Type is āAā for attributes, āVā for nodes with values and empty for all other nodes/elements.
Instead of just displaying the XML table data, the below program moves the XML table data to target student internal table and displays the student table data.
*&---------------------------------------------------------------------* *& Data Types *&---------------------------------------------------------------------* TYPES: BEGIN OF ty_student, id(2), name(20), place(20), END OF ty_student. *&---------------------------------------------------------------------* *& Data Declaration *&---------------------------------------------------------------------* DATA: gcl_xml TYPE REF TO cl_xml_document. DATA: gv_subrc TYPE sy-subrc. DATA: gv_xml_string TYPE xstring. DATA: gv_size TYPE sytabix. DATA: gt_xml_data TYPE TABLE OF smum_xmltb. DATA: gwa_xml_data TYPE smum_xmltb. DATA: gt_return TYPE TABLE OF bapiret2. DATA: gv_tabix TYPE sytabix. DATA: gt_student TYPE TABLE OF ty_student. DATA: gwa_student TYPE ty_student. *&---------------------------------------------------------------------* *& start-of-selection *&---------------------------------------------------------------------* START-OF-SELECTION. CREATE OBJECT gcl_xml. *Upload XML File CALL METHOD gcl_xml->import_from_file EXPORTING filename = 'c:\test.xml' RECEIVING retcode = gv_subrc. IF gv_subrc = 0. CALL METHOD gcl_xml->render_2_xstring IMPORTING retcode = gv_subrc stream = gv_xml_string size = gv_size. IF gv_subrc = 0. * Convert XML to internal table CALL FUNCTION 'SMUM_XML_PARSE' EXPORTING xml_input = gv_xml_string TABLES xml_table = gt_xml_data return = gt_return. ENDIF. ENDIF. WRITE:/ 'ID',6 'Name',26 'Place'. *Convert XML internal table to Target Student table LOOP AT gt_xml_data INTO gwa_xml_data WHERE hier EQ 2. IF gwa_xml_data-type is INITIAL. gv_tabix = sy-tabix + 1. READ TABLE gt_xml_data INTO gwa_xml_data INDEX gv_tabix. gwa_student-id = gwa_xml_data-cvalue. gv_tabix = gv_tabix + 1. READ TABLE gt_xml_data INTO gwa_xml_data INDEX gv_tabix. gwa_student-name = gwa_xml_data-cvalue. gv_tabix = gv_tabix + 1. READ TABLE gt_xml_data INTO gwa_xml_data INDEX gv_tabix. gwa_student-place = gwa_xml_data-cvalue. APPEND gwa_student to gt_student. ENDIF. CLEAR: gwa_xml_data, gwa_student. ENDLOOP. *Display data LOOP AT gt_student INTO gwa_student. WRITE:/ gwa_student-id,6 gwa_student-name,26 gwa_student-place. CLEAR: gwa_student. ENDLOOP.
Output
It works perfectly and helped me a lot!
Thank you so much for this posting!
sir my data in json-xml format how can i get in internal table .
below is the json-xml format.
I tried it, it doesnt workin background mode.
Excellent thanks for the information
is there any function call in abap to convert XML to a structre ?
Hi , I have the same requirement, but I have to use a class instead of the FM SMUM_XML_PARSE, to convert the XSTRING data(XML) into an Internal Table format. My customer coding standards does not allow me to use Function Modules. Can you please tell me what is the equivalent class method for this FM SMUM_XML_PARSE ?
Great program – thanks a lot!