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.
Other versions:
7.31 | 7.40 | 7.54
Source Code
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_conv_codepage=>create_in( )->convert(
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( ).
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 production programs, the APIs of class libraries for XML should be used.