Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Generating an XML Document from an internal table in ABAP

Former Member
0 Kudos

Good day to all of you;

With ABAP, in the R/3 system, I'm trying to figure out a way to accomplish the following:

1) SELECT a set of Purchase Order data into an internal table.

2) Generate an XML document, containing the above data, using a specific schema.

I've been playing around with function module SAP_CONVERT_TO_XML_FORMAT which has the following interface:

CALL FUNCTION 'SAP_CONVERT_TO_XML_FORMAT'

EXPORTING

  • I_FIELD_SEPERATOR = ''

  • I_LINE_HEADER = ''

I_FILENAME = v_fname

  • I_APPL_KEEP = ''

  • I_XML_DOC_NAME = v_docname

IMPORTING

PE_BIN_FILESIZE = v_byte

TABLES

I_TAB_SAP_DATA = i_SapData

CHANGING

I_TAB_CONVERTED_DATA = i_XMLData

EXCEPTIONS

CONVERSION_FAILED = 1

OTHERS = 2.

I'm uncertain as to whether or not the Export parameter, I_XML_DOC_NAME refers to some schema or definition and therefore have been excluding it. In doing so, the generated XML document seems to use the field name/type information from my itab for the tags.

If this function module requires an XML Document Name, how do I create one and where do I store it in R/3? If this is not the recommended solution, is anyone familiar with a way to load an XML schema, retrieve some data then have SAP generate an XML document using the schema?

Many thanks for any help available.

T

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Philips,

You can take a look at the following link

This gives some useful information.

Cheers

VJ

Message was edited by: Vijayendra Rao

9 REPLIES 9

Former Member
0 Kudos

Hi Philips,

You can take a look at the following link

This gives some useful information.

Cheers

VJ

Message was edited by: Vijayendra Rao

Former Member

Hai Phillips

Try with the following Code

This program exports an internal table to an XML file.

*----


*

  • 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 *

*----


*

Thanks & regards

Sreenivasulu P

0 Kudos

it looks like the types that are created, Sreenivasulu, refer directly to what the schema will look like, or at least this is what I'm thinking (from the code you pasted above).

athavanraja
Active Contributor
0 Kudos

if you are on below 4.7 use the method explained in the following weblog.

/people/r.eijpe/blog/2005/11/10/xml-dom-processing-in-abap-part-i--convert-an-abap-table-into-xml-file-using-sap-dom-approach

if you are on 4.7 or above you can simply use ABAP key word

<b>CALL TRANSFORMATION</b> for this prupose.

poitab is the internal table holding the data.
xml_out will hold the result XML (its of type string)

 call transformation (`ID`)
             source pos   = poitab[]
             result xml xml_out.

However, i am not sure about using the schema to validate the XML while creating it. if i find something useful on that i will post it here.

Regards

Raja

0 Kudos

you can validate the generated XML against a DTD

check out this sample program

T_DTD_VALIDATION

also check the programs in package

SIXML_TEST

Regards

Raja

0 Kudos

These are all very useful posts, many thanks for the replies. Herein lies an interesting dilemma, however. We're not using 4.7 (which I should have stated earlier), thus the Transformation cannot be applied.

I don't have a problem making SAP convert my itab into XML (at which point the XML tags will directly map back to my itab field names), but there does not seem to be a way to do it based on <b>a particular schema</b>, where the XML tags correspond to some schema.

Thanks for the help guys, I'm really not looking forward to having to break the tags down manually.

Former Member
0 Kudos

Hi Tavares,

1. itab --- > xml

xml ---> itab.

2. This program will do both.

(just copy paste in new program)

3.

REPORT abc.

*----


DATA

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

DATA : BEGIN OF itab OCCURS 0,

a(100) TYPE c,

END OF itab.

DATA: xml_out TYPE string .

DATA : BEGIN OF upl OCCURS 0,

f(255) TYPE c,

END OF upl.

DATA: xmlupl TYPE string .

                                                              • FIRST PHASE

                                                              • FIRST PHASE

                                                              • FIRST PHASE

*----


Fetch Data

SELECT * FROM t001 INTO TABLE t001.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE tab = t001[]

RESULT XML xml_out.

*----


Convert to TABLE

CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'

EXPORTING

i_string = xml_out

i_tabline_length = 100

TABLES

et_table = itab.

*----


Download

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filetype = 'BIN'

filename = 'd:\xx.xml'

TABLES

data_tab = itab.

                                                              • SECOND PHASE

                                                              • SECOND PHASE

                                                              • SECOND PHASE

BREAK-POINT.

REFRESH t001.

CLEAR t001.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = 'D:\XX.XML'

filetype = 'BIN'

TABLES

data_tab = upl.

LOOP AT upl.

CONCATENATE xmlupl upl-f INTO xmlupl.

ENDLOOP.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE XML xmlupl

RESULT tab = t001[]

.

BREAK-POINT.

regards,

amit m.

Former Member
0 Kudos

hi

have u tried this FM

<b>SMUM_XML_CREATE</b>

if not hav a look.

plz reward if useful

0 Kudos

Unfortunately, this function module does not exist in my version.