Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  asXML - Canonical XML Representation →  asXML - Mapping of ABAP Data Types →  asXML - Mapping of Reference Variables and Referenced Objects 

asXML - Class Instances

To transform classes to XML using the statement CALL TRANSFORMATION, or to generate classes from XML documents, their classes must implement the interface IF_SERIALIZABLE_OBJECT. The instance of a class (object) is displayed as a subelement of heap as follows:

  <asx:heap xmlns:nspace ...>
    <class id = "key">
      <part classVersion = "...">
        <name>...</name>
      </part>
      ...
    </class>
  </asx:heap>

The class element name is the XML schema type name of the class of the object (or the dynamic type of the reference variables) from the nspace namespace (see table below) in uppercase letters. Mandatory attribute id contains unique key key of the element by means of which it is referenced by the display of the corresponding reference variables in values. Subelements <part>...</part> contain the values of the instance attributes of individual object parts as subelements <name>...</name>. The individual object parts are defined by means of the serializable classes of the current inheritance hierarchy (see below).

The namespace of the class name indicates where the class is defined. The table below shows the possible namespaces, whereby in the first column, classes stands for http://www.sap.com/abapxml/classes. For identifiers PRG, CPOOL, and FPOOL, the same substitution rule applies as that for the namespaces for anonymous data objects (see below).

Namespace Where Defined
classes/global Class Library
classes/program/PRG Program prg
classes/class-pool/CPOOL Class Pool cpool
classes/function-pool/FPOOL Function group fpool

The serializable values of an instance of a class (instance attributes or output parameters of a special method, see below) are displayed in the asXML display for named data objects or for reference variables as content or attributes of <name>...</name> whereby name is the name of an instance attribute or output parameter in uppercase letters. For interface attributes, the name of the interface is added before the name, separated by a point (.), to differentiate it from a class attribute of the same name. For the identifiers, the same substitution rules apply as for all elements.

The serializable values of an instance of a class are specified in the class by implementing the system interface IF_SERIALIZABLE_OBJECT. The system interface IF_SERIALIZABLE_OBJECT is a tag interface. Its implementation shows the runtime environment the serializability of a class and allows it to declare certain other components in the class according to fixed syntax rules.

If the class does not implement interface IF_SERIALIZABLE_OBJECT, element class does not contain any subelements. By default, all instance attributes of a class in which interface IF_SERIALIZABLE_OBJECT is implemented are serialized and are deserialized in this class. This behavior can be changed by declaring special help methods (see below). Static attributes are not taken into consideration during serialization or deserialization (with the exception of special constant SERIALIZABLE_CLASS_VERSION, see below).

Other versions: 7.31 | 7.40 | 7.54

Standard Behavior

If the class implements interface IF_SERIALIZABLE_OBJECT, element <class>...</class> contains at least one subelement <part>...</part></class>. These subelements correspond to individual serializable object parts and contain the displays of the instance attributes of the respective object part in asXML format. An object part is specified by the class in which instance attributes are declared or into which an interface that contains instance attributes is integrated. A serializable class contains an object part for itself as well as object parts for all superclasses in the current path of the inheritance tree up to and including the class that implements interface IF_SERIALIZABLE_OBJECT. Name part is the name of the respective class. If the class is a local class, prefix local is added in front of the name, separated by a point (.), to differentiate it from a global class of the same name. Object parts of superclasses in which interface IF_SERIALIZABLE_OBJECT is not implemented are not serializable and do not have a corresponding subelement part. This means that a class in which interface IF_SERIALIZABLE_OBJECT is not implemented (neither in the class itself nor in a superclass) generates an empty XML element class during serialization.

During serialization, the XML elements part of the object part of the superclasses are generated for the subclasses and by default the XML elements of the instance attributes are created in the sequence in which they are declared in the class.

During deserialization, an object of the respective class is generated, whereby the instance constructor is not executed. After the object generation, all instance attributes have their initial value or the start value that is specified with addition VALUE of statement DATA. By default, the instance attributes are supplied with the values of the corresponding XML elements. Here the sequence of the object parts and the attributes is not important. Instance attributes without a corresponding XML element retain their value. Unnecessary XML elements are ignored as long as they do not belong to a namespace; otherwise they generate an exception that can be resolved. During the deserialization of an element that does not have any subelements part, no object is created but instead the target reference variable is initialized.

If a class implements interface IF_SERIALIZABLE_OBJECT, the private constant SERIALIZABLE_CLASS_VERSION of type i can be declared in each object part, that is, in each participating class of the inheritance tree. During serialization, the value of the constant is assigned to attribute classVersion of XML element part. By default, an exception that can be resolved occurs during deserialization if the value of the attribute does not match the value of the constant in the specified class. An object part can only be deserialized if the values match or if neither the attribute nor the constant exists. This behavior can be changed by declaring special help methods.


Note

Using the standard behavior involves a certain security risk, because all instance attributes of an object can be serialized and can be manipulated in the XML file that is created (see example).


Example

The example shows that the standard behavior permits an object of a class to be generated for which a private attribute receives a value set from outside. After deserialization, the value of private attribute attr in the newly-generated class does not match the value in the class.

CLASS cls DEFINITION. 
  PUBLIC SECTION. 
    INTERFACES if_serializable_object. 
  PRIVATE SECTION. 
    DATA attr TYPE string VALUE 'Private'. 
ENDCLASS. 

DATA xml_string TYPE string. 

DATA oref TYPE REF TO cls. 

START-OF-SELECTION. 

  CREATE OBJECT oref. 

  CALL TRANSFORMATION id SOURCE oref = oref 
                      RESULT XML xml_string. 

  REPLACE 'Private' IN xml_string WITH 'Public'. 

  CALL TRANSFORMATION id SOURCE XML xml_string 
                         RESULT oref = oref.

Adjusted Behavior

By default, all instance attributes of an object part are serialized regardless of their visibility and the version of the class is checked. To change this behavior, for each object part, instance methods SERIALIZE_HELPER and DESERIALIZE_HELPER can be declared and implemented in the respective class. These methods can only be declared as private instance methods in classes that implement interface IF_SERIALIZABLE_OBJECT. The declaration of one of the methods restricts the declaration of the others, and the interface is predefined by the syntax check as described below:

  • Method SERIALIZE_HELPER is only allowed to have output parameters, and method DESERIALIZE_HELPER is only allowed to have input parameters with generic typing.
  • For each output parameter of method SERIALIZE_HELPER, there must be an input parameter of method DESERIALIZE_HELPER that has the same name and the same typing. Additional input parameters of method DESERIALIZE_HELPER have to be optional.
  • Method SERIALIZE_HELPER is not allowed to have any output parameters of name SERIALIZABLE_CLASS_VERSION, and method DESERIALIZE_HELPER is allowed to have this optional input parameter of type i. During deserialization, this is supplied with the value of attribute classVersion of element part. Here the standard check of the version (see above) is avoided.

If methods SERIALIZE_HELPER and DESERIALIZE_HELPER are declared in an object part, the instance attributes of the object part are not serialized and deserialized. Instead, during serialization, method SERIALIZE_HELPER is executed and the values of all output parameters are written to the corresponding element part as subelements in the sequence specified in asXML format. The name of a subelement is the name of the corresponding output parameter in uppercase letters. During deserialization, method DESERIALIZE_HELPER is called, whereby the values of the subelements of the corresponding element part are passed to the input parameters of the methods that have the same name. The sequence does not matter here and unnecessary XML elements are ignored.


Example

The example shows how methods SERIALIZE_HELPER and DESERIALIZE_HELPER can be used to avoid an undesirable manipulation of attributes. Private attribute attr is serialized by means of SERIALIZE_HELPER but is not deserialized in DESERIALIZE_HELPER.

CLASS cls DEFINITION. 
  PUBLIC SECTION. 
    INTERFACES if_serializable_object. 
  PRIVATE SECTION. 
    DATA attr TYPE string VALUE 'Private'. 
    METHODS: serialize_helper   EXPORTING attr TYPE string, 
             deserialize_helper IMPORTING attr TYPE string. 
ENDCLASS. 

CLASS cls IMPLEMENTATION. 
  METHOD serialize_helper. 
    attr = me->attr. 
  ENDMETHOD. 
  METHOD deserialize_helper. 
  ENDMETHOD. 
ENDCLASS. 

DATA xml_string TYPE string. 

DATA oref TYPE REF TO cls. 

START-OF-SELECTION. 

  CREATE OBJECT oref. 

  CALL TRANSFORMATION id SOURCE oref = oref 
                        RESULT XML xml_string. 

  REPLACE 'Private' IN xml_string WITH 'Public'. 

  CALL TRANSFORMATION id SOURCE XML xml_string 
                         RESULT oref = oref.