[XI/PI] ABAP Mapping

EAI/XI/PI 2013. 8. 12. 09:53

본 소스는는 "http://www.riyaz.net/sap/abap-mapping-code-walkthrough/862/" Blog에서 가져온 내용입니다.


Abap Mapping을 테스트 할때 사용 하는 Input XML 데이터는 아래와 같다.


Output XML데이터는 아래와 같다.


기본적으로 Operation Mapping에서 ABAP Mapping을 사용하게 하려면 아래 URL을 참고 해서 옵션을 켜둔다.

http://blog.inspien.co.kr/24


ABAP쪽 소스코드는 다음과 같다. 

T-CODE "SE80"으로 들어가서 다음 그림과 같이 Class Library를 생성한다.


Class명과 Description을 넣는다.


Class생성후 Interface Tab에 가서 "IF_MAPPING"을 추가 한다.



Interface를 추가하면 Methods Tab에 가보면 IF_MAPOING~EXECUTE 라는 Methods가 생성되는데 더블클릭해서 들어가면 소스를 입력 할 수 있다.



method IF_MAPPING~EXECUTE.
* initialize iXML
  TYPE-POOLS: ixml.
  CLASS cl_ixml DEFINITION LOAD.
* create iXML factory object
  DATA: ixmlfactory TYPE REF TO if_ixml.
  ixmlfactory = cl_ixml=>create( ).
* create streamfactory object
  DATA: streamfactory TYPE REF TO
  if_ixml_stream_factory.
  streamfactory = ixmlfactory->create_stream_factory( ).
* create input stream object
  DATA: istream TYPE REF TO if_ixml_istream.
  istream = streamfactory->create_istream_xstring( source ).
* initialize the input xml document
  DATA: idocument TYPE REF TO if_ixml_document.
  idocument = ixmlfactory->create_document( ).
* parse the input xml document
  DATA: iparser TYPE REF TO if_ixml_parser.
  iparser = ixmlfactory->create_parser(
  stream_factory = streamfactory
  istream = istream
  document = idocument ).
  iparser->parse( ).

* Getting the Message ID
* Not necessary. Just demonstrating how to access mapping parameters in ABAP mapping.
  DATA: l_msgid_ref TYPE string.
  DATA : oref TYPE REF TO cx_root.
  TRY.
      l_msgid_ref = param->get( if_mapping_param=>message_id ).
    CATCH cx_sy_ref_is_initial INTO oref.
  ENDTRY.
* Reading source XML data
  DATA: odocument TYPE REF TO if_ixml_document.
  DATA: root TYPE REF TO if_ixml_element.
  DATA: msgid TYPE REF TO if_ixml_element.
  DATA: person TYPE REF TO if_ixml_element.
  DATA: name TYPE REF TO if_ixml_element.
  DATA: fname TYPE REF TO if_ixml_node_collection.
  DATA: lname TYPE REF TO if_ixml_node_collection.
  DATA: bdate TYPE REF TO if_ixml_node_collection.
  DATA: firstname TYPE REF TO if_ixml_node.
  DATA: lastname TYPE REF TO if_ixml_node.
  DATA: birthdate TYPE REF TO if_ixml_node.
  DATA: fullname TYPE string.
  DATA: str_fname TYPE string.
  DATA: str_lname TYPE string.
  DATA: fname_iterator TYPE REF TO if_ixml_node_iterator.
  DATA: lname_iterator TYPE REF TO if_ixml_node_iterator.
  DATA: bdate_iterator TYPE REF TO if_ixml_node_iterator.
  DATA: ostream TYPE REF TO if_ixml_ostream.
  DATA: renderer TYPE REF TO if_ixml_renderer.
  DATA: rc TYPE i.
  DATA: len TYPE i.
  DATA: idx TYPE i.
  fname = idocument->get_elements_by_tag_name( 'Name' ).
  lname = idocument->get_elements_by_tag_name( 'Surname' ).
  bdate = idocument->get_elements_by_tag_name( 'Birthdate' ).
* create output document
  odocument = ixmlfactory->create_document( ).
* create a root node Names
  root = odocument->create_simple_element(
  name = 'PersonsCompact'
  parent = odocument ).
* create element 'MessageID' and add it to the output document
  msgid = odocument->create_simple_element(
  name = 'MessageID'
  value = l_msgid_ref
  parent = root ).
* create iterators to iterate over the data elements
  fname_iterator = fname->create_iterator( ).
  lname_iterator = lname->create_iterator( ).
  bdate_iterator = bdate->create_iterator( ).
  len = fname->get_length( ).
  idx = 0.
  WHILE len GT idx.
    idx = idx + 1.
* create element 'Person' and add it to the output document
    person = odocument->create_simple_element(
    name = 'Person'
    parent = root ).
    firstname = fname_iterator->get_next( ).
    lastname = lname_iterator->get_next( ).
    birthdate = bdate_iterator->get_next( ).
    str_fname = firstname->get_value( ).
    str_lname = lastname->get_value( ).
    CONCATENATE str_fname str_lname INTO fullname SEPARATED BY space.
* create element 'Name' and add it to the output document
    name = odocument->create_simple_element(
    name = 'Name'
    value = fullname
    parent = person ).
* create element 'Birthdate' and add it to the output document
    rc = person->append_child( birthdate ).
  ENDWHILE.
* create output stream
  ostream = streamfactory->create_ostream_xstring( result ).
* create renderer
  renderer = ixmlfactory->create_renderer(
  ostream = ostream
  document = odocument ).
  rc = renderer->render( ).


endmethod.



위의 소스를 IF_MAPING~EXECUTE 넣은 후 Active하고 OP에서 다음과 같이 해당 Class 명을 넣으면 끝이다.




Posted by INSPIEN
,