Wednesday, June 12, 2013

How to download IDoc as XML file?

IDocs are sent to the Java Connector(JCo) as an IDoc-XML string. If you want to see how IDoc-XML look like, follow the below steps to download the IDoc to frontend as XML file.
  1. First create a IDoc object for a given idoc number.
  2. Get IDoc XML data as a string.
  3. Download the XML string to frontend.
PARAMETERS: p_idoc TYPE edidd-docnum.

DATA: o_idoc_xml TYPE REF TO cl_idoc_xml1.
DATA: gv_string  TYPE string.
DATA: gt_string  TYPE TABLE OF string.

*Create IDoc object
CREATE OBJECT o_idoc_xml
EXPORTING
docnum = p_idoc
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
  WRITE: /'Error creating idoc object'.
  EXIT.
ENDIF.

*Get IDoc data as string
CALL METHOD o_idoc_xml->get_xmldata_as_string
  IMPORTING
    data_string = gv_string.

APPEND gv_string TO gt_string.

IF sy-subrc NE 0 OR o_idoc_xml IS INITIAL.
  WRITE: /'Error getting xml data as string'.
  EXIT.
ENDIF.

*Download IDoc data as XML file
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename = 'C:\idoc.xml'
  TABLES
    data_tab = gt_string.
Downloaded XML file
ALE-IDoc-1

1 comment:

  1. if you have problems with unicode or long IDOCs not displayed as correct XML try this version:

    REPORT z_xml.
    PARAMETERS: p_idoc TYPE edidd-docnum,
    p_file type STRING default 'C:\TEMP\IDOC.xml'.

    DATA: o_idoc_xml TYPE REF TO cl_idoc_xml1.
    DATA: gv_string TYPE string.
    DATA: gt_string TYPE TABLE OF string.
    DATA: gt_string_xml TYPE TAB_CHAR512.


    *Create IDoc object
    CREATE OBJECT o_idoc_xml
    EXPORTING
    docnum = p_idoc
    EXCEPTIONS
    OTHERS = 1.
    IF sy-subrc NE 0.
    WRITE: /'Error creating idoc object'.
    EXIT.
    ENDIF.

    *Get IDoc data as string
    CALL METHOD o_idoc_xml->GET_XMLDATA_AS_TABLE
    IMPORTING
    data_table = gt_string_xml.


    IF sy-subrc NE 0 OR o_idoc_xml IS INITIAL.
    WRITE: /'Error getting xml data as string'.
    EXIT.
    ENDIF.

    *Download IDoc data as XML file
    CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
    filename = p_file
    FILETYPE = 'ASC'
    write_BOM = 'X'
    codepage = '4103'
    WRITE_LF = ' '
    TRUNC_TRAILING_BLANKS = ' '
    TRUNC_TRAILING_BLANKS_EOL = ' '
    TABLES
    data_tab = gt_string_xml.

    Christian(at) csbg.biz

    ReplyDelete