0% found this document useful (0 votes)
362 views

Generate Idoc To XML

This document describes converting IDOC documents to XML format using ABAP code. It includes: 1. An ABAP function module that transforms an IDOC document number into XML, by loading the IDOC data, determining the appropriate XSLT transformation program, applying the transformation to the IDOC data to convert it to XML, and exporting the XML. 2. Exceptions that can occur in the function module like failure to load the IDOC or XML, or errors in loading the transformation program. 3. An ABAP report that calls the function module to convert an input IDOC document number to XML.

Uploaded by

awad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
362 views

Generate Idoc To XML

This document describes converting IDOC documents to XML format using ABAP code. It includes: 1. An ABAP function module that transforms an IDOC document number into XML, by loading the IDOC data, determining the appropriate XSLT transformation program, applying the transformation to the IDOC data to convert it to XML, and exporting the XML. 2. Exceptions that can occur in the function module like failure to load the IDOC or XML, or errors in loading the transformation program. 3. An ABAP report that calls the function module to convert an input IDOC document number to XML.

Uploaded by

awad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

IDOC TO XML

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

* Function Module *
**********************
Contoh Nama FM: ZIDOC_XML_TRANSFORM

Import
DOCNUM
ACTIONS
STYLEID

TYPE EDI_DOCNUM
TYPE EDI_STFLAG
TYPE EDI_STYLE
Export

ACTION

LIKE

SY-UCOMM

Exception
NO_IDOC_XML_LOADED
NO_IDOC_XML_DISPLAY_AVAILABLE
ERROR_LOADING_OBJECT

Source Code
FUNCTION ZIDOC_XML_TRANSFORM.
*"-------------------------------------------------------------------*"*"Local Interface:
*" IMPORTING
*"
REFERENCE(DOCNUM) TYPE EDI_DOCNUM
*"
REFERENCE(ACTIONS) TYPE EDI_STFLAG OPTIONAL
*"
REFERENCE(STYLEID) TYPE EDI_STYLE OPTIONAL
*" EXPORTING
*"
VALUE(ACTION) LIKE SY-UCOMM
*" EXCEPTIONS
*"
NO_IDOC_XML_LOADED
*"
NO_IDOC_XML_DISPLAY_AVAILABLE
*"
ERROR_LOADING_OBJECT
*"-------------------------------------------------------------------CLASS
CLASS
CLASS
CLASS

cl_ixml
cl_abap_char_utilities
cl_gui_frontend_services
cl_gui_html_viewer

DEFINITION
DEFINITION
DEFINITION
DEFINITION

LOAD.
LOAD.
LOAD.
LOAD.

DATA:
DATA:
DATA:
DATA:

container TYPE REF TO cl_gui_custom_container.


repid LIKE sy-repid, dynnr LIKE sy-dynnr.
wa_obj_location TYPE idocstyle.
local_xslid TYPE edi_style.

DATA:
DATA:
DATA:
DATA:
DATA:

base_url(30).
data_table TYPE tab_raw512.
len TYPE i.
ui_flag TYPE i.
myevent_tab TYPE cntl_simple_events,
myevent TYPE cntl_simple_event.
DATA: local_styleid TYPE edi_style.
DATA: parent TYPE REF TO cl_gui_container.
DATA: line TYPE i.
DATA: wa_src TYPE struct_src.
DATA: local_edidc TYPE edidc.
DATA:
tab_obj_location TYPE STANDARD TABLE OF idocstyle .
DATA: gen_ex TYPE REF TO cx_xslt_exception.
DATA: srcstr TYPE REF TO if_ixml_istream.
*load-of-program.
TRY.
CREATE OBJECT xsltp.
CATCH cx_xslt_exception.
WRITE / text-e01. EXIT. " XSLT not supported
ENDTRY.
g_ixml = cl_ixml=>create( ).
g_stream_factory = g_ixml->create_stream_factory( ).
dir = ''.
CALL METHOD cl_gui_frontend_services=>get_temp_directory
CHANGING
temp_dir = dir.
*
*start-of-selection.
*
global_docnum = docnum.
global_styleid = styleid.
GET PARAMETER ID 'IDOC_STYLEID' FIELD global_styleid.
IF global_styleid IS INITIAL.
global_styleid = 'IE5'.
ENDIF.
CLEAR fcode_tab.
CLEAR global_ok_code.
CLEAR action.
* we only support action "deletemark" + "closeproce"
IF actions <> ' X X '.
fcode_wa = 'DELM'.
APPEND fcode_wa TO fcode_tab.
fcode_wa = 'CLPR'.
APPEND fcode_wa TO fcode_tab.
ENDIF.
* Create Object and create IDOC_XML
CREATE OBJECT idoc
EXPORTING
docnum
= docnum

EXCEPTIONS
error_loading_idoc = 1
error_building_xml = 2
OTHERS
= 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
RAISING no_idoc_xml_loaded.
ENDIF.
* Determine transformation program.
CLEAR tab_obj_location.
SELECT * FROM idocstyle INTO TABLE tab_obj_location
WHERE ( styleid = global_styleid OR styleid = '' )
AND ( mestyp = local_edidc-mestyp OR mestyp = '')
AND ( direct = local_edidc-direct OR direct = '')
AND objnr = 0.
DESCRIBE TABLE tab_obj_location LINES line.
IF line > 0.
LOOP AT tab_obj_location INTO wa_obj_location.
MOVE wa_obj_location-url TO progname.
CLEAR wa_obj_location.
EXIT.
ENDLOOP.
ELSE.
MESSAGE e110(idoc_xml1) WITH local_xslid wa_obj_location-url
RAISING error_loading_object.
ENDIF.
* Transformation
CALL METHOD idoc->get_xmldata_as_string
IMPORTING
data_string = str.
resstr = g_stream_factory->create_ostream_itable(
table = restab ).
* RUN
TRY.
CALL TRANSFORMATION (progname)
SOURCE XML str
RESULT XML resstr.
CATCH cx_xslt_exception INTO gen_ex.
MESSAGE e110(idoc_xml1) WITH local_xslid wa_obj_location-url
RAISING error_loading_object.
EXIT.
ENDTRY.
SPLIT str AT SPACE INTO TABLE t_table.
Note - I am storing my XML file in C: drive
concatenate 'D:\' docnum sy-datum sy-uzeit '.xml' into filename SEPARATED BY
SPACE.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = FILENAME
tables
data_tab = t_table
EXCEPTIONS
*

FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 S
Y-MSGV3 SY-MSGV4.
ENDIF.
*

CALL SCREEN 100.


IF global_ok_code = 'DELM' OR global_ok_code = 'CLPR'.
action = global_ok_code.
ENDIF.
SET PARAMETER ID 'IDOC_STYLEID' FIELD global_styleid.

RETURN.
ENDFUNCTION.

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

* Program SE38 *
********************
*&---------------------------------------------------------------------*
*& Report ZIDOCTOXML
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT

ZIDOCTOXML.

**Tables
Tables: EDIDC.
*Internal Table
DATA T_EDIDC LIKE EDIDC OCCURS 0 WITH HEADER LINE.
*Parameters
PARAMETERS: DOCNUM LIKE EDIDC-DOCNUM.
*F4 help
AT SELECTION-SCREEN ON VALUE-REQUEST FOR DOCNUM.
select * from EDIDC into table t_edidc up to 50 rows.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'DOCNUM'
dynpprog = sy-repid
dynpnr = sy-dynnr
DYNPROFIELD = 'DOCNUM'
value_org = 'S'
TABLES
value_tab = T_EDIDC
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
*Start of Selection
START-OF-SELECTION.
CALL FUNCTION 'ZIDOC_XML_TRANSFORM'
EXPORTING
docnum = DOCNUM.

You might also like