[[{“value”:”
Introduction:
In most SAP ABAP projects, file uploads are a very common requirement—particularly in formats like TXT, CSV, or Excel. These formats are straightforward to parse using standard approaches such as GUI_UPLOAD, CL_GUI_FRONTEND_SERVICES, or transformations.
- However, during a recent requirement, I encountered a different scenario:
the business team wanted to upload an XML file, extract specific fields from the XML structure, convert the values into an ABAP internal table, and finally allow users to download the processed data in CSV format. - While XML is widely used for integrations, parsing XML inside ABAP is not as common in day-to-day development—especially when the XML structure is nested and needs to be converted into a flattened internal table.
This made the requirement both interesting and challenging: - How do we read an XML file from the presentation server?
- How do we parse the XML structure into an easily consumable internal table?
- How do we convert the extracted data into a CSV file dynamically?
- How do we present the parsed data to the user (ALV) before export?
- SAP fortunately provides powerful XML-handling tools such as CL_XML_DOCUMENT and SMUM_XML_PARSE, which help convert XML into a tree-like format that can be processed easily. Using these, we can extract required data nodes (like EmpID, EmpName, Salary) and map them into a custom structure.
This blog explains the complete end-to-end solution:
- Reading XML from local file
- Parsing XML using SMUM_XML_PARSE
- Populating an internal table
- Displaying the result in ALV
- Exporting the final output to CSV
- This approach can be reused for any XML To ABAP conversion scenario, not just employee data. Whether you are processing orders, invoices, or custom data feeds, the same logic applies with minor adjustments.
” Data declarations
DATA: gcl_xml TYPE REF TO cl_xml_document,
gv_xml_string TYPE xstring,
gv_size TYPE sytabix,
gt_xml_data TYPE TABLE OF smum_xmltb,
gwa_xml_data TYPE smum_xmltb,
gt_return TYPE TABLE OF bapiret2.
” Employee structure
TYPES: BEGIN OF ty_employee,
empid TYPE char10,
empname TYPE char50,
salary TYPE char15,
END OF ty_employee.
DATA: gt_employees TYPE TABLE OF ty_employee,
gwa_employee TYPE ty_employee.
” File parameter
PARAMETERS: p_file TYPE ibipparms-path.
” F4 help for file selection
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION ‘F4_FILENAME’
IMPORTING
file_name = p_file.
START-OF-SELECTION.
” Create XML document object
CREATE OBJECT gcl_xml.
” Import XML from file
gcl_xml->import_from_file( filename = p_file ).
IF sy-subrc = 0.
” Render XML to xstring
gcl_xml->render_2_xstring( IMPORTING
stream = gv_xml_string
size = gv_size ).
IF sy-subrc = 0.
” Parse XML
CALL FUNCTION ‘SMUM_XML_PARSE’
EXPORTING
xml_input = gv_xml_string
TABLES
xml_table = gt_xml_data
return = gt_return.
IF sy-subrc = 0.
” Process XML data to extract employee information
LOOP AT gt_xml_data INTO gwa_xml_data.
CASE gwa_xml_data-cname.
WHEN ‘EmpID’.
gwa_employee-empid = gwa_xml_data-cvalue.
WHEN ‘EmpName’.
gwa_employee-empname = gwa_xml_data-cvalue.
WHEN ‘Salary’.
gwa_employee-salary = gwa_xml_data-cvalue.
” Append when all fields are collected
APPEND gwa_employee TO gt_employees.
CLEAR gwa_employee.
ENDCASE.
ENDLOOP.
” Display data using ALV
IF gt_employees IS NOT INITIAL.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = data(lo_alv)
CHANGING
t_table = gt_employees.
” Get columns object
DATA(lo_columns) = lo_alv->get_columns( ).
lo_columns->set_optimize( abap_true ).
” Set column headers
TRY.
DATA(lo_column) = lo_columns->get_column( columnname = ‘EMPID’ ).
lo_column->set_long_text( ‘Employee ID’ ).
lo_column->set_medium_text( ‘Emp ID’ ).
lo_column->set_short_text( ‘EmpID’ ).
CATCH cx_salv_not_found.
ENDTRY.
TRY.
lo_column = lo_columns->get_column( columnname = ‘EMPNAME’ ).
lo_column->set_long_text( ‘Employee Name’ ).
lo_column->set_medium_text( ‘Emp Name’ ).
lo_column->set_short_text( ‘EmpName’ ).
CATCH cx_salv_not_found.
ENDTRY.
TRY.
lo_column = lo_columns->get_column( columnname = ‘SALARY’ ).
lo_column->set_long_text( ‘Employee Salary’ ).
lo_column->set_medium_text( ‘Salary’ ).
lo_column->set_short_text( ‘Salary’ ).
CATCH cx_salv_not_found.
ENDTRY.
” Display ALV
lo_alv->display( ).
CATCH cx_salv_msg.
ENDTRY.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
Data : IOUT TYPE TABLE OF STRING,
XOUT TYPE STRING.
FIELD-SYMBOLS: <FS>.
LOOP AT GT_EMPLOYEES INTO GWA_EMPLOYEE.
CLEAR XOUT.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE GWA_EMPLOYEE TO <FS>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
IF SY-INDEX = 1.
XOUT = <FS>.
ELSE.
CONCATENATE XOUT <FS> INTO XOUT SEPARATED BY ‘,’.
ENDIF.
ENDDO.
APPEND XOUT TO IOUT.
ENDLOOP.
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
FILENAME = ‘C:- Tech_csv.csv’
TABLES
DATA_TAB = IOUT.
Conclusion:
This blog demonstrated a clear and practical approach to reading XML files in ABAP and transforming their contents into structured data using the Factory Method pattern. By separating file parsing, object creation, and data handling, the solution becomes more scalable, maintainable, and easy to extend for future XML formats. This design not only improves code readability but also ensures that enhancements or format changes can be accommodated with minimal impact on existing logic.
“}]]
Read More Technology Blog Posts by Members articles
#abap