ABAP Keyword Documentation → ABAP - Reference → Data Interfaces and Communication Interfaces → ABAP and XML → Simple Transformations → ST - Examples
Simple Transformation for structures
This example demonstrates the symmetrical serialization and deserialization of a nested structure.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA: BEGIN OF struc1,
col1 TYPE c LENGTH 10 VALUE 'ABCDEFGHIJ',
col2 TYPE i VALUE 111,
BEGIN OF struc2,
col1 TYPE d VALUE '20040126',
col2 TYPE t VALUE '084000',
END OF struc2,
END OF struc1.
DATA: xml_string TYPE string,
result LIKE struc1.
TRY.
<span class="blue">* Serialization</span>
CALL TRANSFORMATION demo_simple_transformation
SOURCE para = struc1
RESULT XML xml_string.
<span class="blue">* Display result</span>
cl_abap_browser=>show_xml( EXPORTING xml_string = xml_string ).
<span class="blue">* Deserialization</span>
CALL TRANSFORMATION demo_simple_transformation
SOURCE XML xml_string
RESULT para = result.
<span class="blue">* Check result</span>
IF result = struc1.
MESSAGE 'Symmetric transformation!' TYPE 'I'.
ENDIF.
CATCH cx_st_error.
<span class="blue">* Error handling</span>
MESSAGE 'Error in Simple Transformation'
TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
Description
In the ABAP program, a nested structure struc1
is serialized to xml_string
with the Simple Transformation demo_simple_transformation
and deserialized with the same transformation.
The simple transformation demo_simple_transformation
has the following form:
<tt:transform template="temp"
xmlns:tt="http://www.sap.com/transformation-templates"
version="0.1">
<tt:root name="PARA"/>
<tt:template name="temp">
<X>
<X1>
<tt:value ref="PARA.COL1" />
</X1>
<X2>
<tt:value ref="PARA.COL2" />
</X2>
<X3>
<X1>
<tt:value ref="PARA.STRUC2.COL1" />
</X1>
<X2>
<tt:value ref="PARA.STRUC2.COL2" />
</X2>
</X3>
</X>
</tt:template>
</tt:transform>
The transformation consists of a template temp
that defines the structure
of the XML document and establishes relationships between value nodes and components of the structure.
The result of the transformation is as follows (line breaks and indentations were inserted for clarification purposes):
<X1>ABCDEFGHIJ</X1>
<X2>111</X2>
<X3>
<X1>2004-01-26</X1>
<X2>08:40:00</X2>
</X3>
</X>
The conversion of elementary data types is the same as for
asXML. The reverse transformation generates the same content in the structure result
as in struc1
.