SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

XML



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Function Modules | Функциональные модули
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Tue Sep 18, 2007 10:28 am    Post subject: XML Reply with quote

SDIXML_DATA_TO_DOM - Convert SAP data (elementary/structured/table types) into DOM (XML)
This program exports an internal table to an XML file.

Code:

*----------------------------------------------------------------------*
* Report ZPRUEBA_MML_13 *
* Export an internal table to XML document *
* NO BORRAR ESTE CODIGO *
*----------------------------------------------------------------------*
REPORT ZPRUEBA_MML_13.
*----------------------------------------------------------------------*
* PANTALLA SELECCION *
    PARAMETERS: GK_RUTA TYPE RLGRAP-FILENAME.
* PANTALLA SELECCION *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* TYPE TURNOS *
TYPES: BEGIN OF TURNOS,
    LU LIKE T552A-TPR01,
    MA LIKE T552A-TPR01,
    MI LIKE T552A-TPR01,
    JU LIKE T552A-TPR01,
    VI LIKE T552A-TPR01,
    SA LIKE T552A-TPR01,
    DO LIKE T552A-TPR01,
END OF TURNOS.
* TYPE TURNOS *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* TYPE SOCIO *
TYPES: BEGIN OF SOCIO,
    NUMERO LIKE PERNR-PERNR,
    REPOSICION LIKE PA0050-ZAUVE,
    NOMBRE LIKE PA0002-VORNA,
    TURNOS TYPE TURNOS,
END OF SOCIO.
* TYPE SOCIO *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* ESTRUCTURA ACCESOS *
DATA: BEGIN OF ACCESOS OCCURS 0,
    SOCIO TYPE SOCIO,
END OF ACCESOS.
* ESTRUCTURA ACCESOS *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* START OF SELECTION *
START-OF-SELECTION.
    PERFORM LLENA_ACCESOS.
    PERFORM DESCARGA_XML.
END-OF-SELECTION.
* END OF SELECTION *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* FORM LLENA_ACCESOS *
FORM LLENA_ACCESOS.
REFRESH ACCESOS.
CLEAR ACCESOS.
MOVE: '45050' TO ACCESOS-SOCIO-NUMERO,
              'MOISES MORENO' TO ACCESOS-SOCIO-NOMBRE,
              '0' TO ACCESOS-SOCIO-REPOSICION,
              'T1' TO ACCESOS-SOCIO-TURNOS-LU,
              'T2' TO ACCESOS-SOCIO-TURNOS-MA,
              'T3' TO ACCESOS-SOCIO-TURNOS-MI,
              'T4' TO ACCESOS-SOCIO-TURNOS-JU,
              'T5' TO ACCESOS-SOCIO-TURNOS-VI,
              'T6' TO ACCESOS-SOCIO-TURNOS-SA,
              'T7' TO ACCESOS-SOCIO-TURNOS-DO.
APPEND ACCESOS.
CLEAR ACCESOS.
MOVE: '45051' TO ACCESOS-SOCIO-NUMERO,
              'RUTH PEСA' TO ACCESOS-SOCIO-NOMBRE,
              '0' TO ACCESOS-SOCIO-REPOSICION,
              'T1' TO ACCESOS-SOCIO-TURNOS-LU,
              'T2' TO ACCESOS-SOCIO-TURNOS-MA,
              'T3' TO ACCESOS-SOCIO-TURNOS-MI,
              'T4' TO ACCESOS-SOCIO-TURNOS-JU,
              'T5' TO ACCESOS-SOCIO-TURNOS-VI,
              'T6' TO ACCESOS-SOCIO-TURNOS-SA,
              'T7' TO ACCESOS-SOCIO-TURNOS-DO.
APPEND ACCESOS.
ENDFORM.
* FORM LLENA_ACCESOS *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* FORM DESCARGA_XML *
FORM DESCARGA_XML.
DATA: L_DOM TYPE REF TO IF_IXML_ELEMENT,
              M_DOCUMENT TYPE REF TO IF_IXML_DOCUMENT,
              G_IXML TYPE REF TO IF_IXML,
              W_STRING TYPE XSTRING,
              W_SIZE TYPE I,
              W_RESULT TYPE I,
              W_LINE TYPE STRING,
              IT_XML TYPE DCXMLLINES,
              S_XML LIKE LINE OF IT_XML,
              W_RC LIKE SY-SUBRC.

DATA: XML TYPE DCXMLLINES.
DATA: RC TYPE SY-SUBRC,
BEGIN OF XML_TAB OCCURS 0,
              D LIKE LINE OF XML,
END OF XML_TAB.

CLASS CL_IXML DEFINITION LOAD.
G_IXML = CL_IXML=>CREATE( ).
CHECK NOT G_IXML IS INITIAL.
M_DOCUMENT = G_IXML->CREATE_DOCUMENT( ).
CHECK NOT M_DOCUMENT IS INITIAL.
WRITE: / 'Converting DATA TO DOM 1:'.
CALL FUNCTION 'SDIXML_DATA_TO_DOM'
EXPORTING
              NAME = 'ACCESOS'
              DATAOBJECT = ACCESOS[]
IMPORTING
              DATA_AS_DOM = L_DOM
CHANGING
              DOCUMENT = M_DOCUMENT
EXCEPTIONS
              ILLEGAL_NAME = 1
              OTHERS = 2.
IF SY-SUBRC = 0.
              WRITE 'Ok'.
ELSE.
              WRITE: 'Err =',
              SY-SUBRC.
ENDIF.
CHECK NOT L_DOM IS INITIAL.
W_RC = M_DOCUMENT->APPEND_CHILD( NEW_CHILD = L_DOM ).
IF W_RC IS INITIAL.
              WRITE 'Ok'.
ELSE.
              WRITE: 'Err =',
              W_RC.
ENDIF.
CALL FUNCTION 'SDIXML_DOM_TO_XML'
EXPORTING
              DOCUMENT = M_DOCUMENT
IMPORTING
              XML_AS_STRING = W_STRING
              SIZE = W_SIZE
TABLES
              XML_AS_TABLE = IT_XML
EXCEPTIONS
              NO_DOCUMENT = 1
              OTHERS = 2.
IF SY-SUBRC = 0.
              WRITE 'Ok'.
ELSE.
              WRITE: 'Err =',
              SY-SUBRC.
ENDIF.
LOOP AT IT_XML INTO XML_TAB-D.
              APPEND XML_TAB.
ENDLOOP.
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
              BIN_FILESIZE = W_SIZE
              FILENAME = GK_RUTA
              FILETYPE = 'BIN'
TABLES
              DATA_TAB = XML_TAB
EXCEPTIONS
              OTHERS = 10.
IF SY-SUBRC <> 0.
              MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM.
* FORM DESCARGA_XML *


SDIXML_DOM_TO_XML - Convert DOM (XML) into string of bytes that can be downloaded to PC or application server
Code:

REPORT z_xml_convert.

data: it_table like t001 occurs 0.

data: l_dom TYPE REF TO IF_IXML_ELEMENT,
m_document TYPE REF TO IF_IXML_DOCUMENT,
g_ixml TYPE REF TO IF_IXML,
w_string TYPE XSTRING,
w_size TYPE I,
w_result TYPE I,
w_line TYPE STRING,
it_xml TYPE DCXMLLINES,
s_xml like line of it_xml,
w_rc like sy-subrc.

start-of-selection.
select * from t001 into table it_table.

end-of-selection.
********************************************
**** initialize iXML-Framework ****
********************************************
write: / 'initialiazing iXML:'.
class cl_ixml definition load.
g_ixml = cl_ixml=>create( ).
check not g_ixml is initial.
write: 'ok'.

********************************************
**** create DOM from SAP data ****
********************************************
write: / 'creating iXML doc:'.
m_document = g_ixml->create_document( ).
check not m_document is initial.
write: 'ok'.

write: / 'converting DATA TO DOM 1:'.
CALL FUNCTION 'SDIXML_DATA_TO_DOM'
EXPORTING
NAME = 'IT_TABLE'
DATAOBJECT = it_table[]
IMPORTING
DATA_AS_DOM = l_dom
CHANGING
DOCUMENT = m_document
EXCEPTIONS
ILLEGAL_NAME = 1
OTHERS = 2.
if sy-subrc = 0. write 'ok'.
else. write: 'Err =', sy-subrc.
endif.
check not l_dom is initial.

write: / 'appending DOM to iXML doc:'.
w_rc = m_document->append_child( new_child = l_dom ).
if w_rc is initial. write 'ok'.
else. write: 'Err =', w_rc.
endif.

********************************************
**** visualize iXML (DOM) ****
********************************************
write: / 'displaying DOM:'.
CALL FUNCTION 'SDIXML_DOM_TO_SCREEN'
EXPORTING
DOCUMENT = m_document
EXCEPTIONS
NO_DOCUMENT = 1
OTHERS = 2.
if sy-subrc = 0. write 'ok'.
else. write: 'Err =', sy-subrc.
endif.

********************************************
**** convert DOM to XML doc (table) ****
********************************************
write: / 'converting DOM TO XML:'.
CALL FUNCTION 'SDIXML_DOM_TO_XML'
EXPORTING
DOCUMENT = m_document
* PRETTY_PRINT = ' '
IMPORTING
XML_AS_STRING = w_string
SIZE = w_size
TABLES
XML_AS_TABLE = it_xml
EXCEPTIONS
NO_DOCUMENT = 1
OTHERS = 2.
if sy-subrc = 0. write 'ok'.
else. write: 'Err =', sy-subrc.
endif.

write: / 'XML as string of size:', w_size, / w_string.

describe table it_xml lines w_result.
write: / 'XML as table of', w_result, 'lines:'..
loop at it_xml into s_xml.
write s_xml.
endloop.

write: / 'end of processing'.
* end of code


OOO Example:
Code:

report z.
type-pools truxs.

data: it_table like t001 occurs 0.

data: l_dom TYPE REF TO IF_IXML_ELEMENT,
m_document TYPE REF TO IF_IXML_DOCUMENT,
m_xmldoc TYPE REF TO CL_XML_DOCUMENT,
g_ixml TYPE REF TO IF_IXML,
l_iref_pstreamfactory TYPE REF TO if_ixml_stream_factory,
l_iref_postream TYPE REF TO if_ixml_ostream,
It_CONVERTED_DATA TYPE TRUXS_XML_TABLE,
it_converted_line like line of It_CONVERTED_DATA,
w_result TYPE I,
w_rc like sy-subrc,
s_node type string.

start-of-selection.
select * from t001 into table it_table.

end-of-selection.
********************************************
**** initialize iXML-Framework ****
********************************************
write: / 'initialiazing iXML:'.
class cl_ixml definition load.
g_ixml = cl_ixml=>create( ).
check not g_ixml is initial.
write: 'ok'.

********************************************
**** create DOM from SAP data ****
********************************************
write: / 'creating iXML doc:'.
m_document = g_ixml->create_document( ).
check not m_document is initial.
write: 'ok'.

write: / 'converting DATA TO DOM 1:'.
CALL FUNCTION 'SDIXML_DATA_TO_DOM'
EXPORTING
NAME = 'IT_TABLE'
DATAOBJECT = it_table[]
IMPORTING
DATA_AS_DOM = l_dom
CHANGING
DOCUMENT = m_document
EXCEPTIONS
ILLEGAL_NAME = 1
OTHERS = 2.
if sy-subrc = 0. write 'ok'.
else. write: 'Err =', sy-subrc.
endif.
check not l_dom is initial.

write: / 'appending DOM to iXML doc:'.
w_rc = m_document->append_child( new_child = l_dom ).
if w_rc is initial. write 'ok'.
else. write: 'Err =', w_rc.
endif.

********************************************
**** creating object XML doc ****
********************************************
write: / 'creating XML document:'.
create object m_xmldoc.
check not m_xmldoc is initial.
write 'ok'.

write: / 'getting XML from DOM:'.
w_rc = m_xmldoc->create_with_dom( document = m_document ).
if w_rc is initial. write 'ok'.
else. write: 'Err =', w_rc.
endif.

********************************************
**** visualize object XML doc **************
********************************************
write: / 'displaying XML document:'.
call method m_xmldoc->display.

write: / 'select node from XML document:'.
s_node = m_xmldoc->call_f4( ).
write s_node.

********************************************
**** convert DOM to XML doc (table) ****
**** method 2 - OO ****
********************************************
write: / 'creating stream factory:'.
l_iref_pstreamfactory = g_ixml->create_stream_factory( ).
check not l_iref_pstreamfactory is initial.
write 'ok'.

write: / 'creating ostream table:'.
l_iref_postream = l_iref_pstreamfactory->create_ostream_itable(
table = it_converted_data ).
check not l_iref_postream is initial.
write 'ok'.

write: / 'rendering (filling table):'.
CALL METHOD m_document->render( ostream = l_iref_postream ).
* -- how many bytes were written to the table?
w_result = l_iref_postream->get_num_written_raw( ).
write: / w_result, 'bytes were written to the table:'.
loop at it_converted_data into it_converted_line.
write it_converted_line-data.
endloop.

write: / 'end of processing'.
* end of code


SDIXML_DOM_TO_SCREEN - Display DOM (XML)

SDIXML_DOM_TO_DATA - Convert DOM (XML) into SAP data (elementary/structured/table types)
Code:

*&---------------------------------------------------------------------*
*& Report  ZZXMLTEST3                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZZXMLTEST3                              .

*----------------------------------------------------------------------*
* Report ZPRUEBA_MML_13 *
* Export an internal table to XML document *
* NO BORRAR ESTE CODIGO *
*----------------------------------------------------------------------*
*REPORT ZPRUEBA_MML_13.
*----------------------------------------------------------------------*
* PANTALLA SELECCION *
    PARAMETERS: GK_RUTA TYPE RLGRAP-FILENAME DEFAULT 'C:\DELETE123.XML'.
* PANTALLA SELECCION *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* TYPE TURNOS *
TYPES: BEGIN OF TURNOS,
    LU LIKE T552A-TPR01,
    MA LIKE T552A-TPR01,
    MI LIKE T552A-TPR01,
    JU LIKE T552A-TPR01,
    VI LIKE T552A-TPR01,
    SA LIKE T552A-TPR01,
    DO LIKE T552A-TPR01,
END OF TURNOS.
* TYPE TURNOS *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* TYPE SOCIO *
TYPES: BEGIN OF SOCIO,
    NUMERO LIKE PERNR-PERNR,
    REPOSICION LIKE PA0050-ZAUVE,
    NOMBRE LIKE PA0002-VORNA,
    TURNOS TYPE TURNOS,
END OF SOCIO.
* TYPE SOCIO *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* ESTRUCTURA ACCESOS *
DATA: BEGIN OF ACCESOS OCCURS 0,
    SOCIO TYPE SOCIO,
END OF ACCESOS.
DATA: L_DOM TYPE REF TO IF_IXML_ELEMENT,
              M_DOCUMENT TYPE REF TO IF_IXML_DOCUMENT,
              G_IXML TYPE REF TO IF_IXML,
              W_STRING TYPE XSTRING,
              W_RESULT TYPE I,
              W_LINE TYPE STRING,
              IT_XML TYPE DCXMLLINES,
              S_XML LIKE LINE OF IT_XML,
              W_RC LIKE SY-SUBRC.

DATA: XML TYPE DCXMLLINES.
DATA: RC TYPE SY-SUBRC,
BEGIN OF XML_TAB OCCURS 0,
              D LIKE LINE OF XML,
END OF XML_TAB,
              W_SIZE TYPE I.


* ESTRUCTURA ACCESOS *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* START OF SELECTION *
START-OF-SELECTION.

CALL FUNCTION 'WS_UPLOAD'
 EXPORTING
*   CODEPAGE                      = ' '
   FILENAME                      = GK_RUTA
   FILETYPE                      = 'BIN'
*   HEADLEN                       = ' '
*   LINE_EXIT                     = ' '
*   TRUNCLEN                      = ' '
*   USER_FORM                     = ' '
*   USER_PROG                     = ' '
*   DAT_D_FORMAT                  = ' '
 IMPORTING
   FILELENGTH                    = W_SIZE
  TABLES
    DATA_TAB                      = XML_TAB
 EXCEPTIONS
   CONVERSION_ERROR              = 1
   FILE_OPEN_ERROR               = 2
   FILE_READ_ERROR               = 3
   INVALID_TYPE                  = 4
   NO_BATCH                      = 5
   UNKNOWN_ERROR                 = 6
   INVALID_TABLE_WIDTH           = 7
   GUI_REFUSE_FILETRANSFER       = 8
   CUSTOMER_ERROR                = 9
   NO_AUTHORITY                  = 10
   OTHERS                        = 11
          .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


LOOP AT XML_TAB.

APPEND XML_TAB-D TO IT_XML.

ENDLOOP.


CALL FUNCTION 'SDIXML_XML_TO_DOM'
  EXPORTING
    XML                  = IT_XML
   SIZE                 = W_SIZE
*   IS_NORMALIZING       = 'X'
 IMPORTING
   DOCUMENT             = M_DOCUMENT

 EXCEPTIONS
   INVALID_INPUT        = 1
   OTHERS               = 2
          .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


** @@@ THIS IS WHERE I GET STRUCK @@@ ##

*Fill L_DOM

** @@@ THIS IS WHERE I GET STRUCK @@@ ##

CALL FUNCTION 'SDIXML_DOM_TO_DATA'
  EXPORTING
    DATA_AS_DOM          = L_DOM
*   CONTROL              =
 IMPORTING
   DATAOBJECT           =    ACCESOS[]
*   PROBLEMS             =
 EXCEPTIONS
   ILLEGAL_OBJECT       = 1
   OTHERS               = 2
          .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


IDOC_XML_FROM_FILE - Standard program for inbound processing that imports an XML file from the file system, converts it into IDoc format, and passes it to ALE layer.

SDIXML_SCHEMA_CREATE - Bridge Between ABAP and iXML
Here the basic document structure is created

SDIXML_DDIC_TO_SCHEMA - Convert the DDIC structure to XML Schema
See example: https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/0a575986-0601-0010-7cbe-f17b353c3a28
TEXT_CONVERT_XML_TO_SAP - to transfer XML to int table CONVERT_OTF to convert to PDF
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Function Modules | Функциональные модули All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.