Understanding and working with ABAP Mapping

...Previous

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.

Source Input XML File:

Source Input XML File:
  <?xml version="1.0" encoding="UTF-8" ?> 
- <ns0:MT_Emp_Det xmlns:ns0="http://yash.com/YH203/file2file_abapmapping">
- <EMPLOYEE>
- <PERSONAL>
  <EMPID>001</EMPID> 
  <FIRSTNAME>Santosh</FIRSTNAME> 
  <MIDDLENAME>Kumar</MIDDLENAME> 
  <LASTNAME>K</LASTNAME> 
  <AGE>25</AGE> 
  <GENDER>M</GENDER> 
  </PERSONAL>
- <JOB>
  <COMPANY>XYZ</COMPANY> 
  <DEPARTMENT>SAP</DEPARTMENT> 
  <DESIGNATION>Consultant</DESIGNATION> 
  <BEGINDATE>20050606</BEGINDATE> 
  <ENDDATE /> 
  <SALARY>600000</SALARY> 
  </JOB>
- <PERSONAL>
  <EMPID>002</EMPID> 
  <FIRSTNAME>Bala</FIRSTNAME> 
  <MIDDLENAME>Krishna</MIDDLENAME> 
  <LASTNAME>Reddy</LASTNAME> 
  <AGE>25</AGE> 
  <GENDER>M</GENDER> 
  </PERSONAL>
- <JOB>
  <COMPANY>XYZ</COMPANY> 
  <DEPARTMENT>SAP</DEPARTMENT> 
  <DESIGNATION>Consultant</DESIGNATION> 
  <BEGINDATE>20050606</BEGINDATE> 
  <ENDDATE>20061206</ENDDATE> 
  <SALARY>600000</SALARY> 
  </JOB>
- <JOB>
  <COMPANY>XYZ</COMPANY> 
  <DEPARTMENT>SAP</DEPARTMENT> 
  <DESIGNATION>Consultant</DESIGNATION> 
  <BEGINDATE>20070106</BEGINDATE> 
  <ENDDATE /> 
  <SALARY>800000</SALARY> 
  </JOB>
  </EMPLOYEE>
  </ns0:MT_Emp_Det>

Target output XML File generated:

  <?xml version="1.0" ?> 
- <EMPLOYEES>
- <PERSONAL>
  <FULLNAME>SantoshKumarK</FULLNAME> 
  <AGE>25</AGE> 
  <GENDER>M</GENDER> 
  </PERSONAL>
- <JOB>
  <COMPANYDATA>XYZ*SAP*Consultant*20050606**600000</COMPANYDATA> 
  </JOB>
- <PERSONAL>
  <FULLNAME>BalaKrishnaReddy</FULLNAME> 
  <AGE>25</AGE> 
  <GENDER>M</GENDER> 
  </PERSONAL>
- <JOB>
  <COMPANYDATA>XYZ*SAP*Consultant*20050606*20061206*600000</COMPANYDATA> 
  <COMPANYDATA>XYZ*SAP*Consultant*20070106**800000</COMPANYDATA> 
  </JOB>
  </EMPLOYEES>
 
 

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