UPLOAD XML FILE FROM APPLICATION SERVER

Here is the code for uploading XML file from application server into data base server of SAP.

TYPE-POOLS: ixml. "iXML Library Types
*TABLES : rbkp.

&---------------------------------------------------------------------

* TYPE DECLERATIION

&---------------------------------------------------------------------

TYPES: BEGIN OF type_tabpo,
ebeln TYPE ekko-ebeln, "PO document number
ebelp TYPE ekpo-ebelp, "PO line item
END OF type_tabpo.

TYPES: BEGIN OF type_ekbe,
belnr TYPE rbkp-belnr, "Invoice document
gjahr TYPE rbkp-gjahr, "fiscal year
END OF type_ekbe.

TYPES: BEGIN OF type_invoice,
belnr TYPE rbkp-belnr, "PO document number
gjahr TYPE rbkp-gjahr, "Fiscal Year
rbstat TYPE rbkp-rbstat, "invoice status
END OF type_invoice.

TYPES: BEGIN OF t_xml_line, "Structure for holding XML data
data(256) TYPE x,
END OF t_xml_line.

&---------------------------------------------------------------------

* INTERNAL TABLE DECLERATIION

&---------------------------------------------------------------------

DATA: gi_tabpo TYPE STANDARD TABLE OF type_tabpo,
gi_ekbe TYPE STANDARD TABLE OF type_ekbe,
gi_invoice TYPE STANDARD TABLE OF type_invoice,
gi_bapiret2 TYPE STANDARD TABLE OF bapiret2.

DATA: l_ixml TYPE REF TO if_ixml,
l_streamfactory TYPE REF TO if_ixml_stream_factory.

* l_parser TYPE REF TO if_ixml_parser,
* l_istream TYPE REF TO swif_ixml_istream,
* l_document TYPE REF TO if_ixml_document,
* l_node TYPE REF TO if_ixml_node,
* l_xmldata TYPE string.


*DATA: l_elem TYPE REF TO if_ixml_element,

* l_root_node TYPE REF TO if_ixml_node,
* l_next_node TYPE REF TO if_ixml_node,
* l_name TYPE string,
* l_iterator TYPE REF TO if_ixml_node_iterator.


DATA: l_xml_table TYPE TABLE OF t_xml_line, " XML Table of the structure

l_xml_line TYPE t_xml_line, " Record of structure t_xml_line
l_xml_table_size TYPE i. " XML table size

DATA: l_filename TYPE string.

----------------------------------------------------------------

* WORK AREA DECLARATION

----------------------------------------------------------------

DATA: gw_tabpo TYPE type_tabpo,
gw_ekbe TYPE type_ekbe,
gw_invoice TYPE type_invoice,
gw_bapiret2 TYPE bapiret2.

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

* BEGIN OF SELECTION SCREEN

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

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

PARAMETERS: p_file TYPE pathintern LOWER CASE DEFAULT '/usr/sap/tmp/'.

* Validation of XML file: Only DTD included in XML document is supported


SELECTION-SCREEN END OF BLOCK blk1.

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

* INTIALISATION.

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

INITIALIZATION.

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

* SELECTION SCREEN VALIDATION

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

AT SELECTION-SCREEN.

* To validate p_file is not initial

PERFORM sub_validate_file.

* PERFORM sub_validate_path.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

* Request for filename for xml file from the application server

PERFORM sub_get_filename_appl USING p_file.

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

* START OF SELECTION SCREEN

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

START-OF-SELECTION.

PERFORM sub_fetch_po_details.

PERFORM sub_get_invoice.

PERFORM sub_rel_invoice.

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

* END OF SELECTION SCREEN

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

END-OF-SELECTION.

&---------------------------------------------------------------------
*& Form sub_validate_file
&---------------------------------------------------------------------

* To Validate the file

*
----------------------------------------------------------------------
FORM sub_validate_file .

IF p_file IS INITIAL.
MESSAGE e000. "specify the file path

ENDIF.

ENDFORM. " sub_validate_file

&---------------------------------------------------------------------
*& Form sub_get_filename_appl
&----------------------------------------------------------------------
form sub_get_filename_appl USING l_fname TYPE any.

* DATA: l_fname TYPE filename-fileintern. " File name

*GET THE FILENAME FROM THE APPLICATION SERVER

CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
EXPORTING
directory = l_fname
filemask = '*'
IMPORTING
serverfile = l_fname
EXCEPTIONS
canceled_by_user = 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.
ENDFORM. " sub_get_filename_appl


&---------------------------------------------------------------------
*& Form sub_fetch_po_details
&---------------------------------------------------------------------

* To fetch the PO details from the application server
* Format of file is XML

*----------------------------------------------------------------------

FORM sub_fetch_po_details .

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

* TYPE DECLERATIION

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

l_ixml = cl_ixml=>create( ).

* Creating a stream factory

l_streamfactory = l_ixml->create_stream_factory( ).

PERFORM get_xml_table.

LOOP AT gi_tabpo INTO gw_tabpo.
WRITE:/ gw_tabpo.
ENDLOOP.

ENDFORM. " sub_fetch_po_details

&--------------------------------------------------------------------
*& Form get_xml_table
&--------------------------------------------------------------------

* Read from the xml file

---------------------------------------------------------------------
FORM get_xml_table .

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

* Local variable declarations

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

DATA: l_len TYPE i,
l_len2 TYPE i,
l_tab TYPE tsfixml,
l_content TYPE string,
l_str1 TYPE string,
c_conv TYPE REF TO cl_abap_conv_in_ce,
l_itab TYPE TABLE OF string.

l_filename = p_file.

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

* code to upload data from application server

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

OPEN DATASET l_filename FOR INPUT IN BINARY MODE.
IF sy-subrc 0.
WRITE:/ 'invalid file path'.
ENDIF.
DO.
READ DATASET l_filename INTO l_xml_line.

IF sy-subrc EQ 0.

APPEND l_xml_line TO l_xml_table.

ELSE.
EXIT.
ENDIF.
ENDDO.

CLOSE DATASET l_filename.

* code to find the table size

DESCRIBE TABLE l_xml_table.
l_xml_table_size = ( sy-tleng ) * ( sy-tfill ).

*code to convert hexadecimal to XML

LOOP AT l_xml_table INTO l_xml_line.

c_conv = cl_abap_conv_in_ce=>create( input = l_xml_line-data
replacement
= space ).
c_conv->read( IMPORTING data = l_content len = l_len ).
CONCATENATE l_str1 l_content INTO l_str1.

ENDLOOP.

l_str1 = l_str1+0(l_xml_table_size).
SPLIT l_str1 AT cl_abap_char_utilities=>cr_lf INTO TABLE l_itab.
LOOP AT l_itab INTO l_str1.
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN
l_str1 WITH space.
ENDLOOP.

CALL TRANSFORMATION ('ID') " code to put in internal table
SOURCE XML l_str1
RESULT tab = gi_tabpo[].

ENDFORM. " get_xml_table

The previous post of the blog is regarding COLORING CELLS USING ALV REPORT

No comments:

Post a Comment