Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Transformations for XML →  ST - Simple Transformations →  ST - Examples 

Simple Transformation, 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,
          result LIKE struc1.
    DATA(out) = cl_demo_output=>new( ).
    TRY.
        "Serialization
        CALL TRANSFORMATION demo_st_structure
          SOURCE para = struc1
          RESULT XML data(xml).
        out->write_xml( xml ).
        "Deserialization
        CALL TRANSFORMATION demo_st_structure
          SOURCE XML xml
          RESULT para = result.
        IF result = struc1.
           out->write_text( 'Symmetric transformation!' ).
        ENDIF.
      CATCH cx_st_error.
        out->write_text( 'Error in Simple Transformation' ).
    ENDTRY.
    out->display( ).

Description

In the ABAP program, a nested structure struc1 is serialized to xml_string using the simple transformation DEMO_ST_STRUCTURE and deserialized using the same transformation.

The simple transformation DEMO_ST_STRUCTURE has the following form:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates"
              template="temp" 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>
      <X2>
        <tt:copy ref="PARA.STRUC2"/>
      </X2>
    </X>
  </tt:template>
</tt:transform>

The transformation consists of a template temp that defines the structure of the XML data and establishes relationships between value nodes and components of the structure. The ST statements tt:value and tt:copy are used to serialize and deserialize the structure components. The reverse transformation generates the same content in the structure result as in struc1.