Thursday, 14 November 2013

Converting Char string to Text Element Automatically

Converting Char string to Text Element Automatically


*&---------------------------------------------------------------------*
*& Report  ZSLIN
*&
*&---------------------------------------------------------------------*
*& Report Title            : <Description>
*& SAP Release             : 6.0
*& Author's Name           :
*& Creation Date           : 99/99/9999 (DD/MM/YYYY)
*
*& Description            : <Description explaining Functionality>
*&<Description explaining Functionality>
*&<Description explaining Functionality>
*&<Description explaining Functionality>
*&---------------------------------------------------------------------*
REPORT  ZSLIN.
TYPE-POOLS :SLISSLIN.
TYPESBEGIN OF TY_CODE,
         CODE(5000TYPE C,
       END OF TY_CODE.

DATA:  IT_OLD TYPE STANDARD TABLE OF TY_CODE,
       IT_NEW TYPE STANDARD TABLE OF TY_CODE,
       IT_TEXT_OLD TYPE TABLE OF TEXTPOOL,
       IT_TEXT_NEW TYPE TABLE OF TEXTPOOL,
       WA_TEXT_N TYPE TEXTPOOL,
       GV_KEY TYPE n LENGTH 3,
       W_SAVE(1TYPE C.
******************************************************************
*     S E L E C T I O N   S C R E E N
******************************************************************
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME .
PARAMETERS:
       P_REPORT LIKE SY-REPID OBLIGATORY.   " Program Name
*       P_TABLES    AS CHECKBOX,
*       P_TYPES     AS CHECKBOX,
*       P_DATA      AS CHECKBOX,
*       P_CNSTNT AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK B1.
***********************************************************************
*     AT SELECTION SCREEN
***********************************************************************
*AT SELECTION-SCREEN.
*  IF P_TABLES = '' AND P_TYPES = '' AND P_DATA = '' AND P_CNSTNT = ''.
*    MESSAGE TEXT-001 TYPE 'E'.
*  ENDIF.
***********************************************************************
*     START OF SELECTION
***********************************************************************
START-OF-SELECTION.
***********
  PERFORM CHECK CHANGING W_SAVE.
  CHECK W_SAVE EQ 'X'.
  PERFORM GETDATA.
  PERFORM SAVE.
***********

*&---------------------------------------------------------------------*
*&      Form  CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CHECK CHANGING W_SAVE.
  TYPES:  BEGIN OF TY_PROGDIR,
           NAME TYPE PROGDIR-NAME " ABAP Program Name
           STATE TYPE PROGDIR-STATE " ABAP: Program Status (Active, Saved, Transported...)
         END OF TY_PROGDIR.

  DATA:
          LT_PROGDIR TYPE STANDARD TABLE OF TY_PROGDIR.
  DATA:
         W_FLAG_TYPES TYPE C,
         W_FLAG_TABLES TYPE C,
         W_FLAG_DATA TYPE C,
         W_FLAG_CONSTANT TYPE C,
         W_DEVCLASS TYPE TADIR-DEVCLASS" Package
         W_UNAME TYPE TRDIR-UNAM" Last changed by
         W_LOCK TYPE E071-LOCKFLAG" Lock status or import status of an object entry
*       W_SAVE(1) TYPE C,
         W_SHIFT1(1TYPE VALUE 7,
         W_SHIFT2(2TYPE VALUE 9.
*         W_SAVE(1) TYPE C,
*         W_DEVCLASS TYPE TADIR-DEVCLASS. " Package
* Check to see if the program is active or not.
  SELECT NAME STATE FROM PROGDIR INTO TABLE LT_PROGDIR
  WHERE NAME P_REPORT.
  IF SY-DBCNT > 1.
    MESSAGE TEXT-005 TYPE 'I'.
    W_SAVE ''.
    LEAVE TO LIST-PROCESSING.
  ELSEIF SY-DBCNT 0.
    MESSAGE TEXT-006 TYPE 'I'.
    W_SAVE ''.
    LEAVE TO LIST-PROCESSING.
  ELSE.
* Check to see if the program is a local private object ($TMP)
    SELECT SINGLE DEVCLASS
    FROM  TADIR
    INTO W_DEVCLASS
    WHERE PGMID    'R3TR' AND
          OBJECT   'PROG' AND
          OBJ_NAME P_REPORT.
    IF SY-SUBRC AND W_DEVCLASS '$TMP'.
* Check the development class of the program.
      W_SAVE 'X'.
    ELSE.
      SELECT SINGLE UNAM
      FROM  TRDIR
      INTO W_UNAME
      WHERE  NAME P_REPORT.
* Check the user who  last changed the program.
      IF SY-SUBRC AND W_UNAME <> SY-UNAME.
        W_SAVE ''.
        MESSAGE
        TEXT-007 TYPE 'I'.
        W_SAVE ''.
        LEAVE TO LIST-PROCESSING.
      ELSE.
        SELECT SINGLE LOCKFLAG
        FROM  E071
        INTO W_LOCK
        WHERE PGMID 'R3TR' AND OBJECT 'PROG' )   OR
                 PGMID 'LIMU' AND OBJECT 'REPS' AND
                 OBJ_NAME P_REPORT.
* Check if the program is assisgned to a transport or not.
        IF SY-SUBRC AND W_LOCK 'X'.
          W_SAVE 'X'.
        ELSE.
          MESSAGE  TEXT-008 TYPE 'I'.
          W_SAVE ''.
          LEAVE TO LIST-PROCESSING.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    " CHECK
*&---------------------------------------------------------------------*
*&      Form  GETDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GETDATA .
  READ REPORT P_REPORT INTO IT_OLD.
  IT_NEW[] IT_OLD[].
  READ TEXTPOOL P_REPORT INTO IT_TEXT_OLD.
  IT_TEXT_NEW[] IT_TEXT_OLD[].
  DELETE IT_TEXT_NEW WHERE ID NE 'I' OR KEY CA 'wertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'.
  SORT IT_TEXT_NEW DESCENDING BY KEY.
  READ TABLE IT_TEXT_NEW INTO WA_TEXT_N INDEX 1.
  GV_KEY WA_TEXT_N-KEY.
  FREE IT_TEXT_NEW[].
  CLEAR WA_TEXT_N.
  DATAIT_RESULT     TYPE SLIN_RESULT,                     " OCCURS 0,
        WA_RESULT TYPE SLIN_RESULT WITH HEADER LINE,
        RESULTSTAT TYPE SLIN_RESULT_STAT,
        STATUS TYPE RSLIN.
  TYPES :BEGIN OF ST,
          KEY TYPE STRING,
          VALUE TYPE STRING,
         END OF ST.
  TYPES :BEGIN OF ST1,
          TXT_NO TYPE N LENGTH 3,
          SRC_LINE TYPE N LENGTH 4,
          VALUE TYPE STRING,
         END OF ST1.
  DATALINE TYPE SLIN_LINE,
        ARGS LIKE TABLE OF LINE-ARGS,
        WA_TEXT TYPE STRING,
        WA_AGRS TYPE ST,
        WA_AGRS1 TYPE ST1,
        GT_ARGS TYPE TABLE OF ST1.

  STATUS-X_ALL '' .
  STATUS-FLAGS-X_TXT 'X'.
  CALL FUNCTION 'EXTENDED_PROGRAM_CHECK'
    EXPORTING
      PROGRAM      P_REPORT
      COMMENT_FLAG STATUS-X_ALL
      TEST_FLAGS   STATUS-FLAGS
      CACHE_USE    'A'
    IMPORTING
      RESULT       IT_RESULT
      RESULT_STAT  RESULTSTAT
    EXCEPTIONS
      FATAL_ERROR  0.

  FREE LINE-ARGS[].
  FREE WA_RESULT-LINES[].
  CLEAR :WA_RESULTLINEWA_AGRS.
  LOOP AT IT_RESULT INTO WA_RESULT.
    LOOP AT WA_RESULT-LINES INTO LINE WHERE KEY '&1'.
      LOOP AT LINE-ARGS INTO WA_AGRS.
        WA_TEXT_N-ID 'I'.
        CONDENSE gv_key.
        gv_key gv_key + 1.
        WA_TEXT_N-KEY gv_key.
        WA_TEXT_N-LENGTH strlenWA_AGRS-VALUE ).
        WA_TEXT_N-LENGTH WA_TEXT_N-LENGTH 2.
        WA_TEXT_N-ENTRY WA_AGRS-VALUE."+1(WA_TEXT_N-LENGTH).
*        WA_AGRS1-VALUE = WA_AGRS-VALUE.
        APPEND WA_TEXT_N TO IT_TEXT_NEW.
        CLEAR :WA_TEXT_NWA_AGRS.
      ENDLOOP.
      FREE LINE-ARGS[].
    ENDLOOP.
    FREE WA_RESULT-LINES[].
  ENDLOOP.
  DATA GV_TEXT TYPE STRING.
  LOOP AT IT_TEXT_NEW INTO WA_TEXT_N.
    CLEAR GV_TEXT.
    CONCATENATE 'TEXT' WA_TEXT_N-KEY INTO GV_TEXT SEPARATED BY '-'.
    CONDENSE GV_TEXT.
    REPLACE ALL OCCURRENCES OF REGEX WA_TEXT_N-ENTRY
    IN TABLE IT_NEW WITH GV_TEXT
    RESPECTING CASE.
    WA_TEXT_N-ENTRY WA_TEXT_N-ENTRY+1(WA_TEXT_N-LENGTH).
    MODIFY IT_TEXT_NEW FROM WA_TEXT_N TRANSPORTING ENTRY.
  ENDLOOP.

*  ENDIF.

  LOOP AT IT_TEXT_OLD INTO WA_TEXT_N.
    APPEND WA_TEXT_N TO IT_TEXT_NEW.
    CLEAR WA_TEXT_N.
  ENDLOOP.
  SORT IT_TEXT_NEW.
ENDFORM.                    " GETDATA
*&---------------------------------------------------------------------*
*&      Form  SAVE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SAVE .


  DATA:
         L_MESSAGE TYPE MESSAGE.
  CALL FUNCTION 'PRETTY_PRINTER'
    EXPORTING
      INCTOO                        SPACE
* IMPORTING
*   INDENTATION_MAYBE_WRONG       = INDENTATION_MAYBE_WRONG
    TABLES
      NTEXT                         IT_NEW
      OTEXT                         IT_NEW
   EXCEPTIONS
     ENQUEUE_TABLE_FULL            1
     INCLUDE_ENQUEUED              2
     INCLUDE_READERROR             3
     INCLUDE_WRITEERROR            4
            .

  CALL FUNCTION 'ENQUEUE_E_TRDIR'
   EXPORTING
     MODE_TRDIR           'X'
     NAME                 P_REPORT
*   X_NAME               = ' '
*   _SCOPE               = '2'
*   _WAIT                = ' '
*   _COLLECT             = ' '
   EXCEPTIONS
     FOREIGN_LOCK         1
     SYSTEM_FAILURE       2
            .
  IF SY-SUBRC 0.
    INSERT REPORT P_REPORT FROM IT_NEW.
    IF SY-SUBRC 0.
      INSERT TEXTPOOL P_REPORT FROM IT_TEXT_NEW LANGUAGE SY-langU.
      IF SY-SUBRC 0.
        WRITETEXT-002.
      ELSE.
        WRITETEXT-003.
      ENDIF.
    ELSE.
      ROLLBACK WORK.
      WRITETEXT-003.
    ENDIF.
  ELSEIF SY-SUBRC OR SY-SUBRC OR SY-SUBRC 3.
    CALL FUNCTION 'FORMAT_MESSAGE'
      EXPORTING
        ID        SY-MSGID
        LANG      '-D'
        NO        SY-MSGNO
        V1        SY-MSGV1
        V2        SY-MSGV2
        V3        SY-MSGV3
        V4        SY-MSGV4
      IMPORTING
        MSG       L_MESSAGE
      EXCEPTIONS
        NOT_FOUND 1
        OTHERS    2.
    IF SY-SUBRC 0.
      WRITE:/ L_MESSAGE.
      EXIT.
    ENDIF.
  ENDIF.
* Unlock the program after changing.
  CALL FUNCTION 'DEQUEUE_E_TRDIR'
    EXPORTING
      MODE_TRDIR 'X'
      NAME       P_REPORT.

  FREE :IT_TEXT_NEWIT_TEXT_OLD.
ENDFORM.                    " SAVE

Tuesday, 5 November 2013

Converting XML File to Internal Table


Converting XML File to Internal Table

TYPESBEGIN OF it_dmrr,                                    " OCCURS 0,
       dmrrnumber
(25),              " DMRR No
       plant
(25),                   " Plant
       isdaconfirm
(25),             " Confirmatory DMRR
       dmrrdate
(25),                " Creator Date
       dmrrtime
(25),                " Creator Time
       asnnumber
(25),               " ASN NO
       asndate
(25),                 " ASN Date
      END OF it_dmrr.
DATA:it_dmrr TYPE TABLE OF it_dmrr.
     OPEN DATASET dsn FOR INPUT MESSAGE msg IN TEXT MODE ENCODING DEFAULT.

      
IF sy-subrc 0.

        
DO.

          
CLEAR:wa_xml_tab.
          
READ DATASET dsn INTO wa_xml_tab.
          
IF sy-subrc <> 0.
            
EXIT.
          
ENDIF.

          
CONDENSE wa_xml_tab.
*       Append the contents of the work area to the internal table
          
APPEND wa_xml_tab TO g_t_xml_tab. “ xml table

        
ENDDO.
     ENDIF

**************************
        
DATA str TYPE string.
        
DATA xstr TYPE xstring.                           "#EC NEEDED
        
DATAi_xstring TYPE xstring.                       "#EC NEEDED
        
DATABEGIN OF it_xml OCCURS 1,                     "#EC NEEDED
                
c(255TYPE x,
               
END OF it_xml,
      
" Name-value pairs table rturned by FM SMUM_XML_PARSE
            it_smum_xmltb 
TYPE smum_xmltb OCCURS WITH HEADER LINE,
      
" Table returned by FM SMUM_XML_PARSE for error handling
            it_bapiret2 
TYPE bapiret2 OCCURS WITH HEADER LINE.

        
CLEAR:i_xstring,str.
        
REFRESH:it_smum_xmltb[],it_bapiret2[].
        
LOOP AT g_t_xml_tab INTO wa_xml_tab.
          
CONCATENATE str wa_xml_tab-raw INTO str.
        
ENDLOOP.
*  XML_TABLE_SIZE = lv_size = XSTRLEN( STR )

        
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
          
EXPORTING
            
text           str
*     MIMETYPE       = ' '
*     ENCODING       = ENCODING
         
IMPORTING
           
buffer         i_xstring.
*   EXCEPTIONS
*     FAILED         = 1
        
CALL FUNCTION 'SMUM_XML_PARSE'
          
EXPORTING
            xml_input 
i_xstring
          
TABLES
            xml_table 
it_smum_xmltb
            
return    it_bapiret2.
        
CLEAR str.

        
DATA lv_data TYPE c LENGTH 25.
        lv_data 
'DATA'. “XML Root node Name
        
CONDENSE lv_data.

        
LOOP AT it_smum_xmltb.

          
CASE it_smum_xmltb-cname.
            
WHEN lv_data.
              
APPEND wa_dmrr TO it_dmrr.
              
CLEAR wa_dmrr.
            
WHEN 'DMRRNUMBER'. “ur field name
              wa_dmrr
-dmrrnumber it_smum_xmltb-cvalue.
            
WHEN 'PLANT'. “ur field name
              wa_dmrr
-plant it_smum_xmltb-cvalue.
            
WHEN 'ISDACONFIRM'. “ur field name
              wa_dmrr
-isdaconfirm it_smum_xmltb-cvalue.
            
WHEN 'DMRRDATE'. “ur field name
              wa_dmrr
-dmrrdate it_smum_xmltb-cvalue.
            
WHEN 'DMRRTIME'. “ur field name
              wa_dmrr
-dmrrtime it_smum_xmltb-cvalue.
            
WHEN 'ASNNUMBER'. “ur field name
              wa_dmrr
-asnnumber it_smum_xmltb-cvalue.

          
ENDCASE.

        
ENDLOOP.
      if it_smum_xmltb[]
IS NOT INITIAL
        APPEND wa_dmrr TO it_dmrr.
        
CLEAR wa_dmrr.

        
SORT it_dmrr .
        
DELETE ADJACENT DUPLICATES FROM it_dmrr COMPARING ALL FIELDS.
        
DELETE it_dmrr WHERE dmrrnumber IS INITIAL.
                ENDIF.