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.

No comments:

Post a Comment