Tuesday, July 12, 2011

Downloading internal tables as XML

This is simple program to convert Internal table into XML and download the same in local drive or to FTP.
*&---------------------------------------------------------------------*
*& Report  ZDMS_RETAIL_MASTER
*&
*&---------------------------------------------------------------------*
*& Report Title            : Retail Customer Master
*& SAP Release             : 6.0
*& Author's Name           : Vinoth R
*& Creation Date           : 04.07.2011
*& Description            : List of customers for  Retailers
*&
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZDMS_DISTRIBUTOR_MASTER.

**** Tables
TABLES : knvv.

**** Constant
  CONSTANTS : c_vkorg TYPE knvv-vkorg VALUE '1000',
              c_vtweg TYPE knvv-vtweg VALUE '20',
              c_spart TYPE knvv-spart VALUE '00'.

**** Types Declaration
TYPES : BEGIN OF st_knvv,
  KUNNR type knvv-kunnr,
  VKORG type knvv-vkorg,
  VTWEG type knvv-vtweg,
  SPART type knvv-spart,
  END OF st_knvv.

  TYPES : BEGIN OF st_kna1,
    kunnr TYPE kna1-kunnr,
    land1 TYPE kna1-land1,
    name1 TYPE kna1-name1,
    ort01 TYPE kna1-ort01,
    pstlz TYPE kna1-pstlz,
    stras TYPE kna1-stras,
    REGIO type kna1-REGIO,
    ADRNR type kna1-ADRNR,
    TELF1 type kna1-TELF1,
    END OF st_kna1.

 TYPES : BEGIN OF st_adr6,
    ADDRNUMBER TYPE adr6-ADDRNUMBER,
    SMTP_ADDR type adr6-SMTP_ADDR,
   END OF st_adr6.

  TYPES : BEGIN OF st_customer,
    kunnr TYPE kna1-kunnr,
    land1 TYPE kna1-land1,
    name1 TYPE kna1-name1,
    ort01 TYPE kna1-ort01,
    pstlz TYPE kna1-pstlz,
    stras TYPE kna1-stras,
    REGIO type kna1-REGIO,
    ADRNR type kna1-ADRNR,
    TELF1 type kna1-TELF1,
    SMTP_ADDR type adr6-SMTP_ADDR,
    END OF st_customer.

**** for XML output
  TYPES : BEGIN OF st_cust_xml,
    CUSTOMERID TYPE kna1-kunnr,
    DISTRIBUTORNAME TYPE kna1-name1,
    STREET TYPE kna1-stras,
    CITY TYPE kna1-ort01,
    COUNTRYCODE TYPE kna1-land1,
    PINCODENUMBER TYPE kna1-pstlz,
    REGIONCODE type kna1-REGIO,
    EMAIL type adr6-SMTP_ADDR,
    PHONENUMBER type kna1-TELF1,
    MOBILENUMBER type kna1-TELF1,
    END OF st_cust_xml.

**** Data Declaration

DATA : it_customer TYPE TABLE OF st_customer,
       wa_customer TYPE st_customer,
       it_cust_xml type TABLE OF st_cust_xml,
       wa_cust_xml type st_cust_xml,
       it_knvv TYPE TABLE OF st_knvv,
       wa_knvv TYPE st_knvv,
       it_kna1 TYPE TABLE OF st_kna1,
       wa_kna1 TYPE st_kna1,
       it_adr6 TYPE TABLE OF st_adr6,
       wa_adr6 TYPE st_adr6.

 DATA: DSN(100) VALUE 'D:\vino\customer.xml',
        MSG(100).


  DATA: XML_OUT TYPE STRING .


  DATA: BEGIN OF L_XML_TABLE OCCURS 0,
        LINE(1000),
        END OF L_XML_TABLE.

  DATA : XML_TUNE LIKE L_XML_TABLE OCCURS 0 WITH HEADER LINE.
    DATA :LEN TYPE I,LEN1 TYPE I.

**** data selection

START-OF-SELECTION.

**** from Customer
  SELECT kunnr vkorg vtweg spart
    from knvv
    INTO TABLE it_knvv
    where VKORG eq c_VKORG
  and   VTWEG eq c_vtweg
  and   SPART eq c_spart
  AND   KDGRP in ('01','02').
*  and   a~VKBUR in  ('006','007','008','009').


***For Address

if it_knvv[] is NOT INITIAL.
  SELECT kunnr land1 name1 ort01 pstlz
         stras REGIO ADRNR TELF1
    from kna1
    INTO TABLE it_kna1
    FOR ALL ENTRIES IN it_knvv
    where kunnr eq it_knvv-kunnr.
  endif.

** FOR Mail Adressw

  IF it_kna1[] is NOT INITIAL.
   SELECT ADDRNUMBER SMTP_ADDR
     from adr6
     INTO TABLE it_adr6
     FOR ALL ENTRIES IN it_kna1
     WHERE ADDRNUMBER eq it_kna1-adrnr.
  ENDIF.

*** To XML

  LOOP AT it_knvv INTO wa_knvv.

   wa_cust_xml-CUSTOMERID = wa_knvv-kunnr.
** Read Addresss
    READ TABLE it_kna1 INTO wa_kna1 WITH KEY kunnr = wa_knvv-kunnr.
     IF sy-subrc = 0.
    wa_cust_xml-DISTRIBUTORNAME = wa_kna1-name1.
    wa_cust_xml-STREET = wa_kna1-stras.
    wa_cust_xml-CITY = wa_kna1-ort01.
    wa_cust_xml-COUNTRYCODE = wa_kna1-land1.
    wa_cust_xml-PINCODENUMBER = wa_kna1-pstlz.
    wa_cust_xml-REGIONCODE = wa_kna1-regio.
    wa_cust_xml-PHONENUMBER = wa_kna1-TELF1.
*    wa_cust_xml-MOBILENUMBER =

*** Read Email
    READ TABLE it_adr6 INTO wa_adr6 WITH KEY ADDRNUMBER = wa_kna1-adrnr.
    if sy-subrc = 0 and wa_adr6-SMTP_ADDR is NOT INITIAL.
    wa_cust_xml-EMAIL = wa_adr6-SMTP_ADDR.
     ENDIF.
   endif.
    append wa_cust_xml to it_cust_xml.
clear : wa_kna1,wa_adr6,wa_knvv,wa_cust_xml.
  ENDLOOP.


  LOOP AT IT_cust_xml into wa_cust_xml.
    CALL TRANSFORMATION UWS_SFG_BSP_2_HTML_PREVIEW
    SOURCE ROOT = wa_cust_xml
    RESULT XML XML_OUT.
    L_XML_TABLE-LINE = XML_OUT.
    APPEND L_XML_TABLE.

    CLEAR L_XML_TABLE.
  ENDLOOP.

  LOOP AT L_XML_TABLE.
    IF SY-TABIX GT 1 .
*DESCRIBE FIELD L_XML_TABLE-LINE LENGTH LEN IN CHARACTER MODE.
      LEN = STRLEN( L_XML_TABLE-LINE ).
      XML_TUNE-LINE = L_XML_TABLE-LINE+123(LEN) .
    ELSE.
      XML_TUNE-LINE = L_XML_TABLE-LINE.
    ENDIF.
    APPEND XML_TUNE.
    CLEAR XML_TUNE.
  ENDLOOP.
  CLEAR LEN.
  DESCRIBE TABLE XML_TUNE LINES LEN1.

  LOOP AT XML_TUNE.
    IF SY-TABIX NE LEN1.
      LEN = STRLEN( XML_TUNE-LINE ).
      LEN = LEN - 25.
      XML_TUNE-LINE = XML_TUNE+0(LEN).
      MODIFY XML_TUNE.
    ENDIF.
  ENDLOOP.

  OPEN DATASET DSN FOR OUTPUT MESSAGE MSG IN TEXT MODE ENCODING DEFAULT.


  IF SY-SUBRC NE 0.
    WRITE / MSG.
    STOP.
  ENDIF.
DATA: WA_XML LIKE XML_TUNE.

  LOOP AT XML_TUNE INTO WA_XML.
    TRANSFER WA_XML TO DSN.
  ENDLOOP.

  CLOSE DATASET DSN.

   CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
*   BIN_FILESIZE                    =
      FILENAME                        = 'D:\vino\customer.xml'
     FILETYPE                         = 'BIN'
    TABLES
      DATA_TAB                        = XML_TUNE
   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 SY-MSGV3 SY-MSGV4.
  ENDIF.



No comments:

Post a Comment