Skip to Content
0
Mar 15, 2007 at 05:34 PM

ABAP Mapping: ostream with multiple sibling tags (ixml_document_fragment ?)

48 Views

Hallo,

during ABAP Mapping I have the following problem:

I want to return a message to XI Java stack in the following format:

http://logimoda.com/ns_paxar">

<Recordset>

[... child tags which values are retrieved from fields of each record of an internal table]

</Recordset>

</ns0:MT_FILE_PAXAR_INB>

I want n <Recordset>, one for each record of the internal table "t_etichette", which is populated by:

CALL FUNCTION 'ZXI_ETICHETTE_COMPOSIZIONE_PEZ'

Now, i'm not able to append n sibling <Recordset>, only one is appended to parent <ns0:MT_FILE_PAXAR_INB>.

I guess the solution is ixml_document_fragment or using someway an iteration, but I don't know how...

Any suggestion?

Thanks all in advance:)

PS below my current source code:

*******************************************

METHOD if_mapping~execute .

****************************************************************

  • 1.0 Def. Data Types - Start **********************************

****************************************************************

DATA:

wa_t_richieste TYPE zppst_input_etichette_pezzo,

t_richieste TYPE TABLE OF zppst_input_etichette_pezzo,

wa_t_etichette TYPE zppst_output_etichette_pezzo,

t_etichette TYPE TABLE OF zppst_output_etichette_pezzo,

wa_t_logelab TYPE zppst_esito_etichette_pezzo,

t_logelab TYPE TABLE OF zppst_esito_etichette_pezzo.

****************************************************************

  • 1.1 Def. Data Types - End **********************************

****************************************************************

  • initialize iXML

TYPE-POOLS: ixml.

CLASS cl_ixml DEFINITION LOAD.

  • create main factory

DATA: ixmlfactory TYPE REF TO if_ixml.

ixmlfactory = cl_ixml=>create( ).

  • create stream factory

DATA: streamfactory TYPE REF TO if_ixml_stream_factory.

streamfactory = ixmlfactory->create_stream_factory( ).

  • create input stream

DATA: istream TYPE REF TO if_ixml_istream.

istream = streamfactory->create_istream_xstring( source ).

DATA: val_nodo TYPE string,

nome_nodo TYPE string,

nome_el TYPE string

.

DATA: ta_data TYPE STANDARD TABLE OF ztabxml,

wa_data TYPE ztabxml.

  • parse input document =================================================

  • initialize input document

DATA: idocument TYPE REF TO if_ixml_document.

idocument = ixmlfactory->create_document( ).

  • parse input document

DATA: iparser TYPE REF TO if_ixml_parser.

iparser = ixmlfactory->create_parser( stream_factory = streamfactory

istream = istream

document = idocument ).

iparser->parse( ).

DATA: element TYPE REF TO if_ixml_element.

DATA: articolo TYPE REF TO if_ixml_element.

DATA: collo TYPE REF TO if_ixml_element.

DATA: l_trace TYPE string.

DATA: child TYPE REF TO if_ixml_node.

DATA: nr_art TYPE REF TO if_ixml_node_collection.

DATA: dest TYPE rfcdest,

wa_cust TYPE zxi_cust,

mandt TYPE sy-mandt.

SELECT SINGLE * FROM zxi_cust INTO wa_cust

WHERE parname1 = 'RFC_DEST'

AND parname2 = 'R/3'.

dest = wa_cust-parvalue1.

mandt = dest+7(3).

********************************************************************

  • 1.2 Mapping XML -> items ************************************

********************************************************************

CLEAR nome_nodo.

CLEAR child.

CLEAR ciclo.

element = idocument->find_from_path_ns(

path =

'/RIGA'

default_uri = '' ).

nome_el = element->get_name( ).

ciclo = 'true'.

WHILE ciclo = 'true'.

  • Mandante

MOVE mandt TO wa_t_richieste-mandt.

IF nome_el = 'RIGA'.

child = element->get_first_child( ).

WHILE child IS BOUND.

nome_nodo = child->get_name( ).

val_nodo = child->get_value( ).

CASE nome_nodo.

WHEN 'COD_MSGEXPORT'.

  • Identificativo da Logimoda

MOVE val_nodo TO wa_t_richieste-zcodeidoc.

WHEN 'C0001'.

  • Commessa

MOVE val_nodo TO wa_t_richieste-j_3acomord.

[...]

ENDCASE.

child = child->get_next( ).

ENDWHILE.

APPEND wa_t_richieste TO t_richieste.

CLEAR: wa_t_richieste.

element ?= element->get_next( ).

IF NOT element IS BOUND.

EXIT.

ENDIF.

nome_el = element->get_name( ).

ELSE.

ciclo = 'false'.

ENDIF.

ENDWHILE.

*******************************************************************

  • 1.3 call RFC ************************************************

*******************************************************************

DATA: return TYPE sy-subrc.

DATA: t_return TYPE TABLE OF bapiret2.

DATA: st_return TYPE bapiret2.

DATA: mess_exc TYPE string.

DATA: t_error_text TYPE string.

CLEAR: return.

CLEAR: t_return.

REFRESH: t_return.

CALL FUNCTION 'ZXI_ETICHETTE_COMPOSIZIONE_PEZ'

DESTINATION

dest

  • EXPORTING

  • TO DO: Y or N ??

  • fl_alv = ' '

TABLES

t_richieste = t_richieste

t_etichette = t_etichette

t_logelab = t_logelab

EXCEPTIONS

no_richieste = 1

no_etichette = 2

no_plant = 3

OTHERS = 4.

IF ( ( sy-subrc <> 0 ) AND

( dest <> 'MZ1CLNT400' )

).

DATA subrc TYPE sy-subrc.

CASE subrc.

WHEN 1.

  • LOOP AT t_logelab INTO st_logelab WHERE type EQ 'E'.

CONCATENATE 'ABAP MAPPING ERROR: '

'NO_RICHIESTE'

INTO mess_exc.

trace->trace( level = '1'

message = mess_exc ).

RAISE EXCEPTION TYPE cx_mapping_fault

EXPORTING error_code = '90'

error_text = 'NO_RICHIESTE'.

.

  • ENDLOOP.

WHEN 2.

[...]

WHEN 3.

[...]

WHEN OTHERS.

[...]

ENDCASE.

ENDIF.

************************************************************************

  • 1.4 build up output document *****************************************

************************************************************************

DATA: ret_value TYPE i.

DATA: uri TYPE string.

uri = 'http://logimoda.com/ns_paxar'.

  • create output document

DATA: odocument TYPE REF TO if_ixml_document.

odocument = ixmlfactory->create_document( ).

************************************

  • Test - Start ***********

  • DATA: root_ns TYPE REF TO if_ixml_namespace_decl.

  • root_ns = odocument->create_namespace_decl(

  • name = 'ns0'

  • prefix = 'ns0'

  • uri = uri ).

  • ret_value = odocument->set_namespace_prefix(

  • prefix = 'ns0'

  • ).

*

  • ret_value = odocument->set_namespace_uri(

  • uri = 'ns0'

  • ).

*).

  • Test - End ***********

************************************

  • create root XML tag

DATA: root TYPE REF TO if_ixml_element.

root = odocument->create_simple_element_ns(

name = 'MT_FILE_PAXAR_INB'

prefix = 'ns0'

uri = uri

  • value = temp

parent = odocument ).

DATA: ns_xml_attr TYPE REF TO if_ixml_attribute.

ns_xml_attr = odocument->create_attribute_ns(

name = 'ns0'

prefix = 'xmlns'

uri = 'http://logimoda.com/ns_paxar'

).

ret_value = ns_xml_attr->set_value( uri ).

ret_value = root->set_attribute_node_ns(

new_attr = ns_xml_attr

).

  • create recordset tag (<= see ref. in FTP Comm. Channel)

DATA: recordset TYPE REF TO if_ixml_element.

  • recordset = odocument->create_simple_element(

  • name = 'Recordset'

  • value = temp

  • parent = root ).

  • ************************************

    • Test - Start ***********

    DATA: doc_fragm TYPE REF TO if_ixml_document_fragment.

    doc_fragm = odocument->create_document_fragment( ).

    • create recordset tag (<= see ref. in FTP Comm. Channel)

    recordset = odocument->create_simple_element(

    name = 'Recordset'

    • value = temp

    parent = doc_fragm ).

    DATA: ref_child TYPE REF TO if_ixml_node.

    ret_value = root->insert_child(

    new_child = doc_fragm

    ref_child = ref_child

    ).

    • DATA: recordset_array TYPE REF TO if_ixml_node_collection.

    • recordset_array->append_item(recordset_el).

    • Test - End ***********

    ************************************

    *************************

    • XI Inbound Message: MT_FILE_PAXAR_INB

    *************************

    DATA: zcodeidoc TYPE REF TO if_ixml_element.

    DATA: j_3acomord TYPE REF TO if_ixml_element.

    DATA: werks TYPE REF TO if_ixml_element.

    DATA: trilog_yseason TYPE REF TO if_ixml_element.

    [...]

    *************************

    DATA: temp TYPE string.

    LOOP AT t_etichette INTO wa_t_etichette.

    • HERE IS THE PROBLEM, HOW TO DO IT ?

    ret_value = root->insert_child(

    new_child = recordset

    ref_child = recordset

    ).

    CLEAR temp.

    MOVE wa_t_etichette-zcodeidoc TO temp.

    zcodeidoc = odocument->create_simple_element(

    name = 'ZCODEIDOC'

    value = temp

    parent = recordset ).

    CLEAR temp.

    MOVE wa_t_etichette-j_3acomord TO temp.

    j_3acomord = odocument->create_simple_element(

    name = 'j_3acomord'

    value = temp

    parent = recordset ).

    CLEAR temp.

    MOVE wa_t_etichette-werks TO temp.

    werks = odocument->create_simple_element(

    name = 'WERKS'

    value = temp

    parent = recordset ).

    CLEAR temp.

    • MOVE wa_t_etichette-trilog_yseason TO temp.

    trilog_yseason = odocument->create_simple_element(

    name = 'trilog_yseason'

    value = temp

    parent = recordset ).

    [...]

    CLEAR wa_t_etichette.

    ENDLOOP.

    *************************

    • render document ======================================================

    • create output stream

    DATA: ostream TYPE REF TO if_ixml_ostream.

    ostream = streamfactory->create_ostream_xstring( result ).

    • create renderer

    DATA: renderer TYPE REF TO if_ixml_renderer.

    DATA irc TYPE i.

    renderer = ixmlfactory->create_renderer( ostream = ostream

    document = odocument ).

    irc = renderer->render( ).

    ************************************************************************

    • 1.4 for debug ********************************************************

    ************************************************************************

    DATA: conv_out TYPE REF TO cl_abap_conv_out_ce.

    conv_out = cl_abap_conv_out_ce=>create(

    encoding = 'UTF-8'

    endian = 'L' ).

    • conversion string => xstring *********************************

    • conv->convert( EXPORTING data = out

    • IMPORTING buffer = result ).

    • conversion xstring => string *********************************

    DATA: outxml TYPE string.

    DATA: conv_in TYPE REF TO cl_abap_conv_in_ce.

    conv_in = cl_abap_conv_in_ce=>create( input = result ).

    conv_in->read( IMPORTING data = outxml ).

    ************************************************************************

    • store file txt on PC for test purpose - Start ********

    DATA: dataset_str_xml TYPE string,

    debug_allowed(1) TYPE c,

    l_xml_size TYPE i.

    dataset_str_xml = '/usr/sap/XIT/ZXI_PARAX_TEST_XML.xml'.

    OPEN DATASET dataset_str_xml FOR OUTPUT

    IN TEXT MODE ENCODING DEFAULT.

    TRANSFER outxml TO dataset_str_xml.

    CLOSE DATASET dataset_str_xml.

    • store file txt on PC for test purpose - End ********

    ************************************************************************

    ENDMETHOD.