Skip to Content

XML DOM Processing in ABAP part IIIb– XML DOM within SAP XI ABAP Mapping.

This part is the follow up of part IIIa

Step 5 – Integration Directory

The picture below shows the result after executing the actions in the following paragraphs.



Step 5.1 – Configuration Scenario

  • Create a Configuration scenario:

    Configuration scenario : NL4BDemoFlatToXml

    Description            : NL4B Demo Flat To Xml

Step 5.2 – Business Service

  1. Create a Business Service (for receiving the XML file in a FTP share)

  2. Select on the Receiver tab the Inbound Interface

    Inbound Interface    : miiaXmlStructure
  3. Create a Communication Channel:

    CommunicationChannel : ccB_FTP_Receiver

    Transport Protocol   : File Transfer Protocol

    Target diretory      : /xitrans/demo_flat_to_xml

    File Name Scheme     : xml_output.xml

    Server               : <<your FTP server for example:>>



Step 5.3 – Receiver determination

  1. Create a Receiver Determination

    Service : sA_HTTP_Sender

    Interface : mioaFlatStructure

    Namespace : urn:com.nl4b.demo.FlatStructure

  2. Add Configured Receiver

    See Interface Determination and Receiver Agreement

    Interface : miiXmlStructure

    Mapping : imFlat_To_Xml

    Receiver Communication Channel : ccB_FTP_Receiver



Step 5.4 – Interface determination

  1. Create Interface Determination


    Service : sA_HTTP_Sender

    Interface : mioaFlatStructure

    Namespace : urn:com.nl4b.demo.FlatStructure


    Service : sB_FTP_Receiver

  2. Add Inbound Interface


Step 5.5 – Receiver Agreement

  1. Create Receiver Agreement


    Service : sA_HTTP_Sender


    Service : sB_FTP_Receiver

    Interface : miiaXmlStructure

    Namespace : urn:com.nl4b.demo.FlatStructure

    Receiver Communication Channel: ccB_FTP_Receiver


A complete Example

  Source File

We want to send the following flatfile:

HTML Client Adapter

Mapping program method ‘IF_MAPPING~EXECUTE'<br>

  METHOD if_mapping~execute .

  1. initialize iXML

  TYPE-POOLS: ixml.

  DATA: lr_encoding TYPE REF TO if_ixml_encoding.

  1. create main factory

  DATA: lr_ixmlfactory TYPE REF TO if_ixml.
  lr_ixmlfactory = cl_ixml=>create( ).

  1. create stream factory

  DATA: lr_streamfactory TYPE REF TO if_ixml_stream_factory.
  lr_streamfactory = lr_ixmlfactory->create_stream_factory( ).

  1. create input stream

  DATA: lr_istream TYPE REF TO if_ixml_istream.
  lr_istream = lr_streamfactory->create_istream_xstring( source ).

  1. parse input document =================================================
  2. initialize input document

  DATA: lr_idocument TYPE REF TO if_ixml_document.
  lr_idocument = lr_ixmlfactory->create_document( ).

  1. parse input document

  DATA: lr_iparser TYPE REF TO if_ixml_parser.
  lr_iparser = lr_ixmlfactory->create_parser(
               stream_factory = lr_streamfactory
               istream        = lr_istream
               document       = lr_idocument ).
  lr_iparser->parse( ).

  1. get encoding for output stream

  lr_encoding = lr_idocument->get_encoding( ).

  DATA: lr_root TYPE REF TO if_ixml_node.
  DATA: lv_name TYPE string.
  DATA: lt_records TYPE TABLE OF string.

  lr_root = lr_idocument->get_root_element( ).
  lv_name = lr_root->get_name( ).


  1. SPLIT string to records

  DATA lv_value       TYPE string.
  DATA: lv_split      TYPE c LENGTH 2.
  lv_split = cl_abap_char_utilities=>cr_lf.
    lv_value = lr_root->get_value( ).

  1.   set trace message in trace (see transaction SXMB_MONI)

    trace->trace( level = ‘1’ message = lv_value ).
    SPLIT lv_value AT lv_split INTO TABLE lt_records.
    RAISE EXCEPTION TYPE cx_mapping_fault.

  1. build up output document =============================================
  2. create output document

  DATA: lr_odocument TYPE REF TO if_ixml_document.
  lr_odocument = lr_ixmlfactory->create_document( ).

  1. create element ‘SenderService’ and add it to the document

  DATA lr_root_element TYPE REF TO if_ixml_element.
  DATA lv_rc           TYPE i.

  lr_root_element = lr_odocument->create_simple_element_ns(
                       name   = ‘mtXmlStructure’
                       prefix = ‘p2’
                       uri    = ‘urn:com.nl4b.demo.FlatStructure’
                       parent = lr_odocument ).

  lv_rc = lr_root_element->set_attribute(
                         name   = ‘xmlns:p2’
                         value  = ‘urn:com.nl4b.demo.FlatStructure’ ).

  1. map source data to target xml dom object

    BEGIN OF lts_record,
      abbr            TYPE char3,
      descr           TYPE char40,
    END  OF lts_record.
  DATA: ls_record      TYPE lts_record.
  DATA: lr_elem_module TYPE REF TO if_ixml_element.
  DATA: lv_record      TYPE string.

  LOOP AT lt_records INTO lv_record.
    CONDENSE lv_record.
    CLEAR ls_record.
    ls_record = lv_record.

    trace->trace( level = ‘1’ message = lv_record ).

  1.   create element module

    lr_elem_module = lr_odocument->create_simple_element(
                           name   = ‘module’
                           parent = lr_root_element ).

  1.   set abbreviation

    CLEAR lv_value.
    lv_value = ls_record-abbr.
                  name   = ‘abbreviation’
                  value  = lv_value
                  parent = lr_elem_module ).

  1.   set description

    CLEAR lv_value.
    lv_value = ls_record-descr.
                   name   = ‘description’
                   value  = lv_value
                   parent = lr_elem_module ).

  1. render document ======================================================
  2. create output stream

  DATA: lr_ostream TYPE REF TO if_ixml_ostream.
  lr_ostream = lr_streamfactory->create_ostream_xstring( result ).

  1. create renderer

  lr_odocument->set_encoding( encoding = lr_encoding ).
  DATA: lr_renderer TYPE REF TO if_ixml_renderer.
  lr_renderer = lr_ixmlfactory->create_renderer(
                ostream = lr_ostream document = lr_odocument ).
  lv_rc = lr_renderer->render( ).

  1. get XI header data (here: “Sender Service”)

  DATA: lv_sender_service TYPE string.
  lv_sender_service = param->get( if_mapping_param=>sender_service ).

  1. add trace (appears in message monitoring)

  DATA: lv_trace TYPE string.
     ‘Ending abap mapping for Sender Service = ‘
     lv_sender_service INTO lv_trace.
  trace->trace( level = ‘1’  message = lv_trace ).


Result file<br>



As alternative you can also use Simple Transformations or XSLT within your ABAP mapping to convert the flat file into an XML file.


Special thanks to Alwin v.d. Put for helping me writing this weblog.

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