Introduction

The following code snippet shows how to convert string encoding in ABAP using the functionalities of package SIXML (iXML Library: Parsing, DOM Representation, and XML Rendering).

Code Snippet(s)

DATA:
      lv_content_string TYPE string, "the source string
      lv_target_encoding TYPE string DEFAULT 'UTF-8',
      lr_ixml           TYPE REF TO if_ixml,
      lr_stream_factory TYPE REF TO if_ixml_stream_factory,
      lr_encoding       TYPE REF TO if_ixml_encoding,
      lr_resstream      TYPE REF TO if_ixml_ostream,
      lv_content_xstring TYPE xstring. "the result string as binary string
      
    CLEAR rv_content_xstring.
*   Instanciate iXML object.
    lr_ixml = cl_ixml=>create( ).
*   Instanciate a stream factory.
    lr_stream_factory = lr_ixml->create_stream_factory( ).
*   Setup target encoding information.
    lr_encoding = lr_ixml->create_encoding(
      character_set = lv_target_encoding
      byte_order = 0 ).
*   Create the stream specifying where to push the output.
    lr_resstream = lr_stream_factory->create_ostream_xstring( lv_content_xstring ).
*   Set the output encoding.
    CALL METHOD lr_resstream->set_encoding( lr_encoding ).
*   Transform the data encoding.
    CALL TRANSFORMATION id
      SOURCE XML lv_content_string
      RESULT XML lr_resstream.

Explanation

The functionalities of class CL_IXML are used to convert the string encoding via stream processing.

Availability

Key Value
Software Component SAP_BASIS
To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Jacques Nomssi

    Hello Johannes,

    for pure code page conversion check class CL_ABAP_CONV_OUT_CE, i.e.

    cl_abap_conv_out_ce=>create( encoding = ‘UTF-8’ )->convert(

      EXPORTING data = iv_string
      IMPORTING buffer = rv_xstring ).


    best regards,


    JNN

    (0) 

Leave a Reply