Understanding and working with ABAP Mapping


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 =


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.



Ø       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

NAME = "Name of the Element"
PARENT = "Give the Parent Element, here PARENTNODE"
VALUE = "Value of the node, type string "
RVAL = "Returns the created 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.

Click here to continue...


Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image