ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Transformations for XML →  CALL TRANSFORMATION →  CALL TRANSFORMATION - Examples → 

Deserializing Empty Elements

This example demonstrates the deserialization of empty elements.

Source Code

REPORT demo_asxml_asjson_empty_vals.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA(out) = cl_demo_output=>new(
      )->begin_section( `asXML` ).
    DATA elem TYPE i VALUE 111.
    DATA: BEGIN OF struc,
           col TYPE i VALUE 111,
          END OF struc.
    DATA itab TYPE TABLE OF i.
    itab = VALUE #( ( 111 ) ).
    out->begin_section( `ABAP Data Objects`
      )->write_data( elem
      )->write_data( struc
      )->write_data( itab ).

    out->next_section(
      `ABAP Data Objects serialized to asXML` ).
    DATA xml TYPE string.
    CALL TRANSFORMATION id SOURCE elem = elem
                                  struc = struc
                                  itab = itab
                           RESULT XML xml.
    out->write_xml( xml ).

    out->next_section( `Modified asXML` ).
    REPLACE `<ELEM>111</ELEM>` IN xml WITH `<ELEM />`.
    REPLACE `<STRUC><COL>111</COL></STRUC>` IN xml WITH `<STRUC />`.
    REPLACE `<ITAB><item>111</item></ITAB>` IN xml WITH `<ITAB />`.
    out->write_xml( xml ).

    out->next_section(
      `ABAP Data Objects after Deserialization of asXML` ).
    CALL TRANSFORMATION id SOURCE XML xml
                           RESULT elem = elem
                                  struc = struc
                                  itab = itab.
    out->write_data( elem
      )->write_data( struc
      )->write_data( itab
      )->end_section( ).

    out->next_section(
      `ABAP Data Objects after Deserialization with Clearing` ).
    CALL TRANSFORMATION id SOURCE XML xml
                           RESULT elem = elem
                                  struc = struc
                                  itab = itab
                          OPTIONS clear = 'all'.
    out->write_data( elem
      )->write_data( struc
      )->write_data( itab
      )->end_section( ).

    out->next_section( `asJSON` ).
    elem = 111.
    struc-col = 111.
    itab = VALUE #( ( 111 ) ).
    out->begin_section( `ABAP Data Objects`
      )->write_data( elem
      )->write_data( struc
      )->write_data( itab ).

    out->next_section(
      `ABAP Data Objects serialized to asJSON` ).
    DATA(writer) = cl_sxml_string_writer=>create(
                     type = if_sxml=>co_xt_json ).
    CALL TRANSFORMATION id SOURCE elem = elem
                                  struc = struc
                                  itab = itab
                           RESULT XML writer.
    DATA(json) = cl_abap_codepage=>convert_from(
                   writer->get_output( ) ).
    out->write_json( json ).

    out->next_section( `Modified asJSON` ).
    REPLACE `"ELEM":111` IN json WITH `"ELEM":0`.
    REPLACE `"COL":111` IN json WITH ``.
    REPLACE `111` IN json WITH ``.
    out->write_json( json ).

    out->next_section(
      `ABAP Data Objects after Deserialization of asJSON` ).
    CALL TRANSFORMATION id SOURCE XML json
                           RESULT elem = elem
                                  struc = struc
                                  itab = itab.
    out->write_data( elem
      )->write_data( struc
      )->write_data( itab ).

    out->next_section(
      `ABAP Data Objects after Deserialization with Clearing` ).
    CALL TRANSFORMATION id SOURCE XML json
                           RESULT elem = elem
                                  struc = struc
                                  itab = itab
                          OPTIONS clear = 'all'.
    out->write_data( elem
      )->write_data( struc
      )->write_data( itab
      )->display( ).

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

An elementary data object, a structure and an internal table are transformed using the identity transformation ID into asXML and asJSON display formats. With asXML display format, the values of all elements are removed. With asJSON display format, only the components from objects and arrays can be removed.

After deserialization into the original ABAP data objects, the elementary data object and the internal table are initial. However, the empty element in the structure is interpreted as a missing component and the structure component retains its previous value. If the transformation option clear is used with the value "all", all ABAP data objects are initialized.

Note

The modification of XML and JSON data using string processing is only shown here to make the example clearer. In productive programs, the APIs of class libraries for XML should be used.