Monday, July 24, 2017

How to read data in XML form via ABAP?

In one of my blog posts about Intermediate result of APD process a question was raised via comments. User asked where is ABAP code used in APD processes stored. Of course I replied back with the table name (RSANT_PROCESS) where the code can be found. Then the user replied back with another comment saying that field XML of that table is not completely readable as only first 131 characters is visible.

What can be visible in the XML field if the table if it is browsed via t-code like SE11 is something like:

This is really not complete ABAP code I mean not complete content of the field. The thing is that the database fields in xml format needs to be read differently.  It needs to decoded to human readable format. For this we can employ few SAP standard function modules. These are following ones:

SCMS_STRING_TO_XSTRING – converts texts (XML) to binary format, delivered within SAP SCMS (Content Management Service)

SMUM_XML_PARSE - parsing XML document into a table structure, delivered within User Management of SAP Markets (it was SAP initiative around year 2000 which later merged with SAP Portals).

Here complete example on how to read ABAP code for particular APD process in SAP BW system:


DATA: ls_rsant_process TYPE rsant_process,
      lv_xml           TYPE string,
      ls_xml_xstr      TYPE xstring,
      lt_result_xml    TYPE STANDARD TABLE OF smum_xmltb,
      ls_result_xml    TYPE smum_xmltb,
      lt_ret           TYPE STANDARD TABLE OF bapiret2,
      lo_alv           TYPE REF TO cl_salv_table,
      lo_col           TYPE REF TO cl_salv_columns_table,
      lo_fun           TYPE REF TO cl_salv_functions_list.

PARAMETERS: p_apd TYPE rsan_process OBLIGATORY.

SELECT SINGLE * FROM rsant_process INTO ls_rsant_process WHERE objvers = 'A' AND process = p_apd.
lv_xml = ls_rsant_process-xml.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text           = lv_xml
      mimetype       = 'text/xml'
   IMPORTING
     buffer         = ls_xml_xstr
   EXCEPTIONS
     failed         = 1
     others         = 2.

CALL FUNCTION 'SMUM_XML_PARSE'
  EXPORTING
    xml_input       = ls_xml_xstr
  TABLES
    xml_table       = lt_result_xml
    return          = lt_ret.

CALL METHOD cl_salv_table=>factory
  IMPORTING r_salv_table = lo_alv
  CHANGING t_table = lt_result_xml.

lo_col = lo_alv->get_columns( ).
lo_col->set_optimize( ) .
lo_fun = lo_alv->get_functions( ).
lo_fun->set_all( ).
CALL METHOD lo_alv->display.


Source code available at: github.com/softy12/ZMM_READ_XML_DATA

No comments: