Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Transformations for XML →  asXML - Canonical XML Representation →  asXML, Examples of Mappings 

asXML, Deserialization of Structure Components

This example demonstrates the deserialization of empty and missing structure components.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(out) = cl_demo_output=>new(
      )->begin_section( `ABAP Structure` ).
    CONSTANTS:
      BEGIN OF struct,
        text0 TYPE string VALUE ``,
        text1 TYPE string VALUE `Text1`,
        text2 TYPE string VALUE `Text2`,
        text3 TYPE string VALUE `Text3`,
        number0 TYPE i VALUE 0,
        number1 TYPE i VALUE 111,
        number2 TYPE i VALUE 222,
        number3 TYPE i VALUE 333,
     END OF struct.
    DATA(examples) = struct.
    out->write_data( examples ).

    out->next_section( `Structure serialized to asXML` ).
    DATA xml TYPE string.

    CALL TRANSFORMATION id SOURCE examples = examples
                           RESULT XML xml.
    out->write_xml( xml ).

    out->next_section( `Structure serialized to asJSON` ).
    DATA(writer) = cl_sxml_string_writer=>create(
                     type = if_sxml=>co_xt_json ).
    CALL TRANSFORMATION id SOURCE examples = examples
                           RESULT XML writer.
    DATA(json) = cl_abap_conv_codepage=>create_in( )->convert(
                   writer->get_output( ) ).
    out->write_json( json ).

    out->next_section( `Modified asXML` ).
    REPLACE `<TEXT2>Text2</TEXT2>` IN xml WITH `<TEXT2 />`.
    REPLACE `<TEXT3>Text3</TEXT3>` IN xml WITH ``.
    REPLACE `<NUMBER2>222</NUMBER2>` IN xml WITH `<NUMBER2 />`.
    REPLACE `<NUMBER3>333</NUMBER3>` IN xml WITH ``.
    out->write_xml( xml ).

    out->next_section(
      `ABAP Structure after Deserialization of asXML` ).
    examples = struct.
    CALL TRANSFORMATION id SOURCE XML xml
                           RESULT examples = examples.
    out->write_data( examples ).

    out->next_section( `Modified asJSON` ).
    REPLACE `"TEXT2":"Text2",` IN json WITH `"TEXT2":""`.
    REPLACE `"TEXT3":"Text3"` IN json WITH ``.
    REPLACE `"NUMBER2":222,` IN json WITH `"NUMBER2":0`.
    REPLACE `"NUMBER3":333` IN json WITH ``.
    out->write_json( json ).

    out->next_section(
      `ABAP Structure after Deserialization of asJSON` ).
    examples = struct.
    CALL TRANSFORMATION id SOURCE XML json
                           RESULT examples = examples.
    out->write_data( examples
      )->display( ).

Description

A structure is transformed using the identity transformation ID into asXML and asJSON display formats. With asXML display format, the values of two elements and two complete elements are removed. With JSON, values cannot be removed. With asJSON display format, the values of two object components and two object components are removed.

After deserialization into the original ABAP structure, the components with empty XML elements assigned to them are initial. Components, for which no XML elements or no JSON object components exist, retain their previous value.