ABAP Mapping Steps


SAP PI Interview Questions and SAP PI Tutorials

SAP PI Interview Questions and SAP PI Tutorials

ABAP Mapping Steps

This document gives you clear understanding of ABAP Mapping, how to enable the option to use ABAP Mapping class in XI Interface Mappings, how to parse and read the input XML source structure and build the output XML target structure.

Below are different ways of achieving this conversion in XI:

  1. Message Mapping (Graphical Mapping using Mapping Editor in XI)
  2. Java Mapping
  3. ABAP Mapping
  4. XSLT Mapping

The rest of the document gives you pre-requisites and steps that are necessary for making use of ABAP Mapping class.

Pre-requisites and Steps required for developing ABAP Mapping Classes

Pre-requisites: Enable ABAP Mapping:

Add additional mapping types in your exchange profile

  • Open http://<host>:<port>/exchangeProfile/index.html
  • Choose IntegrationBuilder ->IntegrationBuilder.Repository -> com.sap.aii.repository.mapping.additionaltypes
  • Maintain the Entry R3_ABAP|Abapclass;R3_XSLT|XSL (ABAPEngine)

Check whether the data has been successfully read from the exchange profile:

  • Open http://<host>:<port>/rep/support/admin/index.html
  • Choose Administration of Properties -> All Properties. If the value associated to parameter com.sap.aii.repository.mapping.additionaltypes is not correct, Choose at the top of the page and refresh.

Steps to create an ABAP Mapping Class:

  • Create a new ABAP class in transaction SE24 of ABAP Stack of XI,
  • The ABAP Class has to implement the interface IF_MAPPING
  • The method EXECUTE of this interface needs to be developed, this method will be called from during mapping, the method has the following parameters:*  The SOURCE parameter contains the input XML structure in XSTRING format. * The mapping program needs to construct the RESULT xstring, which will be used for the construction of output XML structure. * The explanation of the ABAP Mapping is provided later in the document in the section Steps to be followed in the ABAP Mapping Class.

Scenario Demonstrating the usage of ABAP Mapping

The scenario here involves the conversion of a customer format xml file containing the information of the employees in one specific format to another format. The source file is available on the FTP and the Sender file adapter picks it. This file is deleted and archived on a different directory on the FTP. The source XML file is converted into target XML file by the ABAP mapping program and this target file is placed on the FTP again on a different directory.

OBJECTS DEVELOPED FOR THE SCENARIO
Ø       The following are the object details that are developed for this scenario:

Objects developed in the Integration Repository o        DATA TYPES o        MESSAGE TYPE o        MESSAGE INTERFACES
o        MESSAGE MAPPING

§         No Message Mapping is required as we could directly specify the mapping object as ABAP Mapping class in the Interface mapping object.   o        INTERFACE MAPPING
Objects created in the Integration Directory

SAP PI Interview Questions and SAP PI Tutorials

SAP PI Interview Questions and SAP PI Tutorials

o        Business Service YASH_FTP (the same business service is used as Sender and Receiver )

o        Communication Channels

§         CC_XML_Sender_FIle     §         CC_XML_Receiver_FIle

Steps to be followed in ABAP Mapping classes

Any ABAP Mapping Class will basically consist of three steps:

  • To parse the input xstring SOURCE
  • Traverse through the nodes of the input XML and read the values of those nodes and copy that data to ABAP internal tables and work areas.
  • Construct the output XML Stream from the internal tables and work areas, which contain the collected XML, input data.

To parse the input xstring SOURCE

Ø       The first step is to parse the input XML document, for this we need to create a new parser instance and implement the DOM generating interface to the created parser instance.

The below are the interfaces and their methods used in creating the parser instance. Parse input document

  • Initialize Input Document
    data: idocument type ref to if_ixml_document.
  • creates a new Document instance and returns an interface pointer to this instance.
    idocument = ixml_factory->create_document( ).
    data: iparser type ref to if_ixml_parser.
  • creates a new Parser instance and returns an interface pointer to this instance.
    iparser = ixml_factory->create_parser( stream_factory = stream_factory
    istream = istream
    document = idocument ).
  • implements the DOM-generating interface to the parser
    iparser->parse( ).

From the above we understand that the pre-requisites to create a new parser instance are a Stream factory, an XML Input stream to parse and a document instance into which the XML input stream is to be parsed.

Ø       To create a stream factory, first step is to create a Main factory, i.e. instance of IXML class using the method create of the class if_ixml.

* Create Main Factory

data: ixml_factory type ref to if_ixml.

* creates an instance of the iXML class and returns an interface pointer to the instance.

ixml_factory = cl_ixml=>create( ).

 

* Next Create Stream Factory

* creates a new StreamFactory instance and returns an interface pointer to this instance

data: stream_factory type ref to if_ixml_stream_factory.

stream_factory = ixml_factory->create_stream_factory( ).

Ø       To create Input stream we need the above Stream Factory instance created

* Create Input Stream

data: istream type ref to if_ixml_istream.

* creates a new XML input stream for the given ABAP xstring

istream = stream_factory->create_istream_xstring( SOURCE ).

Where SOURCE is the input XML string.

Now the second step, Traverse through the nodes of the input XML

Ø       Now we need to traverse through the nodes of the input XML string and get the values of each node and store them in ABAP work areas or internal tables which will later be used in constructing the target XML structure

Ø       For this we use Node interfaces like

o       if_ixml_node

§         The iXMLNode object is the primary datatype for the entire Document Object Model. It represents a single node in the document tree.

o        if_ixml_node_list

§         The iXMLNodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented.

o        if_ixml_node_collection

§         Objects implementing the iXMLNodeCollection interface are used to represent collections of nodes that can be accessed by index rather than name.

o        if_ixml_node_iterator

§         An iterator allows the nodes of a data structure to be returned sequentially. When an iterator is first created, calling getNext () returns the first node. When no more nodes are present, getNext () returns null.

Ø       Here our source XML structure will contain a number of nodes in multiple hierarchical structure but we know the parent node and so we should start traversing from the parent node.

Ø       Get the collection of the parent node as below:

Our parent node in the demo scenario is ‘EMPLOYEE’,

* Returns an iXMLNodeCollection of all the elements with a given tag name

* in the order in which they would be encountered in a preorder

* traversal of the document tree.

emp_node_collection =

IDOCUMENT->GET_ELEMENTS_BY_TAG_NAME( NAME = ‘EMPLOYEE’ ).

Where emp_node_collection is of type IF_IXML_NODE_COLLECTION

Ø       Next step is we need to create a node iterator as below,  which will traverse the above Node collection from left to right

emp_node_iterator = emp_node_collection->CREATE_ITERATOR( ).

WHERE emp_node_iterator is of type IF_IXML_NODE_ITERATOR

Ø       Loop thorugh each node and get the name of the structure node

emp_node =   emp_node_iterator->GET_NEXT( ).

WHERE emp_node is of type IF_IXML_NODE

Ø       Create a NODE LIST instance to traverse through the child nodes under this node, use the following method

emp_node_list  = emp_node->GET_CHILDREN( ).

WHERE emp_node_list is of type ref to IF_IXML_NODE_LIST

Ø       Now get the number of nodes in the above NODE LIST structure as below:

emp_node_list_length  = emp_node_list ->GET_LENGTH( ).

WHERE emp_node_list_length is of type I

Ø       Now to traverse to each node inside the above node list collection and get the name and value of each node the following methods need to be used, since there are emp_node_list_length number of child node we need to loop for emp_node_list_length times

*  Loop through as many child nodes as there are for the structure

DO emp_node_list_length TIMES.

*Here we get access to the subnodes i.e. PERSONAL and JOB, and so as explained earlier we need to again collect the node list of each of these nodes, get their children nodes list and finally get access to the element nodes present in these children nodes.

emp_subnode = emp_node_list_iterator->get_next( ).

emp_subnode_name = emp_subnode->get_name( ).

emp_subnode_list = emp_subnode->get_children( ).

emp_subnode_list_length = emp_subnode_list->get_length().

DO emp_subnode_list_length TIMES.

w_index = sy-index – 1.

w_node = emp_subnode_list->get_item( w_index ).

w_node_name  =  w_node->get_name( ).

w_node_value  = w_node->get_value( ).

* Check the name of the node and move the value into the corresponding ABAP work area.

ENDDO.

ENDDO.

Ø       As described above we could traverse through the nodes and get those values, once the values are moved to our ABAP work areas, we need to construct the target XML string

The third step is to Construct the output XML Stream.

Ø       Create an output document

* Build up Output Document

* Create Output Document

data: odocument type ref to if_ixml_document.

* creates a new Document instance and returns an interface pointer to this instance.

odocument = ixml_factory->create_document( ).

Ø       Now to create an element in the above created output document instance , use the following method: Create element ‘EMPLOYEES’ and add it to the document

data: PARENTNODE type ref to if_ixml_element.

Creates a simple element with the given name (and namespace) and the specified value as text content. Note that the instance returned implements the iXMLElement interface, so attributes can be specified directly on the returned object.
PARENTNODE = odocument->create_simple_element( name = ‘EMPLOYEES’ parent = odocument ).
Ø       Now to create a child node for the above PARENTNODE

CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
EXPORTING
NAME = “Name of the Element”
PARENT = “Give the Parent Element, here PARENTNODE”
VALUE = “Value of the node, type string ”
RECEIVING
RVAL = “Returns the created Element”
.
The PARENT and RVAL of type IF_IXML_ELEMENT

Ø      As above we could continue adding elements with desired names and values and hierarchy by looping through the internal tables that we collected by parsing the input document.

Ø      Once the ODOCUMENT is constructed, we need to create the output stream and render the document

* render document

* create output stream

data: ostream type ref to if_ixml_ostream.

ostream = stream_factory->create_ostream_xstring( result ).

WHERE result is the output XML string

Ø       Create renderer

* create renderer
data: renderer type ref to if_ixml_renderer.
renderer = ixml_factory->create_renderer( ostream = ostream
document = odocument ).
renderer->render( ).

This would complete the mapping.

ABAP Class Code

Here is the ABAP Mapping Class Code i.e. developed for reference:
**********************************************************************
method IF_MAPPING~EXECUTE. data:
emp_node_collection type ref to if_ixml_node_collection,
emp_node_length type i,
emp_node_iterator type ref to if_ixml_node_iterator,
emp_node type ref to if_ixml_node,
emp_node_list type ref to if_ixml_node_list,
emp_node_list_length type i,
emp_node_list_iterator type ref to if_ixml_node_iterator,
emp_subnode type ref to if_ixml_node,
emp_subnode_list type ref to if_ixml_node_list,
emp_subnode_list_length type i,
emp_subnode_name type string,
w_node_name type string,
w_node_value type string,
w_fieldname type string,
w_tablename type string,
w_index type i,
w_node type ref to if_ixml_node.
data:
odocument type ref to if_ixml_document,
fs_output type string,
employee_node type ref to if_ixml_node,
personal_node type ref to if_ixml_node,
job_node type ref to if_ixml_node,
rnode type ref to if_ixml_node.
data:
begin of fs_job,
empid type string,
company type string,
department type string,
designation type string,
begindate type string,
enddate type string,
salary type string,
end of fs_job,
begin of fs_personal,
empid type string,
firstname type string,
middlename type string,
lastname type string,
age type i,
gender type string,
end of fs_personal,
t_personal like standard table of fs_personal,
t_job like standard table of fs_job.
field-symbols:
<fs> type any,
<fs_table> type table.

  • Initialize iXML
    type-pools ixml.
    class cl_ixml definition load.
  • create main factory
    data: ixml_factory type ref to if_ixml.
    ixml_factory = cl_ixml=>create( ).
  • create stream factory
    data: stream_factory type ref to if_ixml_stream_factory.
    stream_factory = ixml_factory->create_stream_factory( ).
  • create input stream
    data: istream type ref to if_ixml_istream.
    istream = stream_factory->create_istream_xstring( source ).
  • create input document
    data : idocument type ref to if_ixml_document.
  • initialize parser
    idocument = ixml_factory->create_document( ).
  • Parse the input document
    data iparser type ref to if_ixml_parser.
    iparser = ixml_factory->create_parser( stream_factory = stream_factory
    istream = istream
    document = idocument ).
  • Implements the DOM generating interface to the parser
    iparser->parse( ).
    emp_node_collection = idocument->get_elements_by_tag_name_ns( name = ‘EMPLOYEE’ ).
    emp_node_length = emp_node_collection->get_length( ).
    emp_node_iterator = emp_node_collection->create_iterator( ).
    DO emp_node_length times.
    emp_node = emp_node_iterator->get_next( ).
    emp_node_list = emp_node->get_children( ).
    emp_node_list_length = emp_node_list->get_length( ).
    emp_node_list_iterator = emp_node_list->create_iterator( ).
    DO emp_node_list_length TIMES.
    emp_subnode = emp_node_list_iterator->get_next( ).
    emp_subnode_name = emp_subnode->get_name( ).
    emp_subnode_list = emp_subnode->get_children( ).
    emp_subnode_list_length = emp_subnode_list->get_length( ).
    DO emp_subnode_list_length TIMES.
    w_index = sy-index – 1.
    w_node = emp_subnode_list->get_item( w_index ).
    w_node_name = w_node->get_name( ).
    w_node_value = w_node->get_value( ).
    concatenate ‘fs_’ emp_subnode_name ‘-‘ w_node_name
    into w_fieldname.
    assign (w_fieldname) to <fs>.
    if sy-subrc eq 0.
    move w_node_value to <fs>.
    endif.
    ENDDO.
    concatenate ‘fs_’ emp_subnode_name into w_fieldname.
    assign (w_fieldname) to <fs>.
    check sy-subrc eq 0.
    concatenate ‘t_’ emp_subnode_name into w_tablename.
    assign (w_tablename) to <fs_table>.
    if sy-subrc eq 0.
    move fs_personal-empid to fs_job-empid.
    append <fs> to <fs_table>.
    endif.
    ENDDO.
    ENDDO.
    odocument = ixml_factory->create_document( ).
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘EMPLOYEES’
    PARENT = ODOCUMENT
  • PREFIX = ”
  • URI = ”
  • VALUE = ”
    RECEIVING
    RVAL = EMPLOYEE_NODE
    .
    loop at t_personal into fs_personal.
    clear fs_output.
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘PERSONAL’
    PARENT = EMPLOYEE_NODE
  • PREFIX = ”
  • URI = ”
    VALUE = fs_output
    RECEIVING
    RVAL = PERSONAL_NODE
    .
    concatenate fs_personal-firstname
    fs_personal-middlename
    fs_personal-lastname
    into fs_output.
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘FULLNAME’
    PARENT = personal_node
  • PREFIX = ”
  • URI = ”
    VALUE = fs_output
    RECEIVING
    RVAL = RNODE
    .
    move fs_personal-age to fs_output.
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘AGE’
    PARENT = personal_node
  • PREFIX = ”
  • URI = ”
    VALUE = fs_output
    RECEIVING
    RVAL = RNODE
    .
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘GENDER’
    PARENT = personal_node
  • PREFIX = ”
  • URI = ”
    VALUE = fs_personal-gender
    RECEIVING
    RVAL = RNODE.
    loop at t_job into fs_job where empid = fs_personal-empid.
    AT NEW EMPID.
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘JOB’
    PARENT = EMPLOYEE_NODE
  • PREFIX = ”
  • URI = ”
  • VALUE = ”
    RECEIVING
    RVAL = JOB_NODE.
    ENDAT.
    concatenate fs_job-company
    fs_job-department
    fs_job-designation
    fs_job-begindate
    fs_job-enddate
    fs_job-salary
    into fs_output
    separated by ‘*’.
    CALL METHOD ODOCUMENT->CREATE_SIMPLE_ELEMENT_NS
    EXPORTING
    NAME = ‘COMPANYDATA’
    PARENT = JOB_NODE
  • PREFIX = ”
  • URI = ”
    VALUE = fs_output
    RECEIVING
    RVAL = RNODE.
    endloop.
    endloop.
  • create output stream
    data ostream type ref to if_ixml_ostream.
    ostream = stream_factory->create_ostream_xstring( result ).
  • create renderer
    data renderer type ref to if_ixml_renderer.
    renderer = ixml_factory->create_renderer( ostream = ostream
    document = odocument ).
  • implements DOM based interface to the renderer.
    renderer->render( ).
    endmethod.

Testing of ABAP Mapping Class

The above-created Mapping program could be tested by using the transaction XI_MAPPING_TEST.

This transaction requires the Sender details i.e. Sender Service/Party Name, Sender Interface Namespace, Sender Interface; the Receiver details i.e. Receiver Service/Party Name, Receiver Interface Namespace, Receiver Interface And Input XML source file.

 

SAP PI Interview Questions and SAP PI Tutorials

SAP PI Interview Questions and SAP PI Tutorials

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter
www.pdf24.org    Send article as PDF   
This entry was posted in ABAP Mapping Steps, sap pi 7.0 training, Step by Step Guides and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *