How To Get The Records from the XML File 

PERFORM F_PARSE_FILE USING WV_PIFILE
                    CHANGING WT_XML_TABLE
                             WT_ERRDATA .
*READ DATA FROM TEMPORARY INTERNAL TABLE TO FINAL STRUCTURE
PERFORM F_CREATE_FILE USING WT_XML_TABLE
                       CHANGING WA_LISTE_EVT  .


FORM F_PARSE_FILE    USING FPU_PIFILE TYPE STRING
                  CHANGING FPC_WT_XML TYPE TT_XML_TABLE
                           FPC_WT_ERRDATA TYPE TT_ERRDATA.

  DATA: LO_PIXML          TYPE REF TO IF_IXML,
        LO_PDOCUMENT      TYPE REF TO IF_IXML_DOCUMENT,
        LO_PSTREAMFACTORY TYPE REF TO IF_IXML_STREAM_FACTORY,
        LO_PISTREAM       TYPE REF TO IF_IXML_ISTREAM,
        LO_PPARSER        TYPE REF TO IF_IXML_PARSER,
        LO_PNODE          TYPE REF TO IF_IXML_NODE,
        LO_PPARSEERROR    TYPE REF TO IF_IXML_PARSE_ERROR,
        WLV_STRING        TYPE STRING,
        WLV_COUNT         TYPE I,
        WLV_INDEX         TYPE I,
        WLV_TOTALSIZE     TYPE I,
        WLT_XML_C         TYPE STANDARD TABLE OF T_XML_C,
        WLT_XML_X         TYPE STANDARD TABLE OF T_XML_X,
        WLV_XSTRING       TYPE XSTRING,
        WLA_ERRDATA       TYPE T_ERRDATA.

*READ PHYSICAL FILE TO AN INTERNAL TABLE
  PERFORM F_READ_FILE USING FPU_PIFILE
                   CHANGING WLT_XML_C .
*CONVERT TEXT TO XSTRING FORMAT
  IF NOT WLT_XML_C IS INITIAL.
    CALL FUNCTION 'SCMS_TEXT_TO_XSTRING'
      IMPORTING
        BUFFER   = WLV_XSTRING
      TABLES
        TEXT_TAB = WLT_XML_C
      EXCEPTIONS
        FAILED   = 1.

    IF SY-SUBRC <> 0.
      MESSAGE E030 WITH P_FILE.
    ENDIF.

  ELSE.
    MESSAGE E020 WITH P_FILE.
  ENDIF.
*Convert xstring to binary
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      BUFFER        = WLV_XSTRING
    IMPORTING
      OUTPUT_LENGTH = WLV_TOTALSIZE
    TABLES
      BINARY_TAB    = WLT_XML_X.
*-- CREATE THE MAIN FACTORY
  LO_PIXML = CL_IXML=>CREATE( ).

*-- CREATE THE INITIAL DOCUMENT
  LO_PDOCUMENT = LO_PIXML->CREATE_DOCUMENT( ).

*-- CREATE THE STREAM FACTORY
  LO_PSTREAMFACTORY = LO_PIXML->CREATE_STREAM_FACTORY( ).

*-- CREATE AN INPUT STREAM FOR THE TABLE
  LO_PISTREAM = LO_PSTREAMFACTORY->CREATE_ISTREAM_ITABLE( TABLE =
  WLT_XML_X
                                                          SIZE  =
                                                          WLV_TOTALSIZE
                                                          ).
*-- CREATE THE PARSER
  LO_PPARSER = LO_PIXML->CREATE_PARSER( STREAM_FACTORY =
  LO_PSTREAMFACTORY
                                        ISTREAM        = LO_PISTREAM
                                        DOCUMENT       = LO_PDOCUMENT ).
*-- PARSE THE STREAM
  IF LO_PPARSER->PARSE( ) NE 0.
*Error handling for parser object
    IF LO_PPARSER->NUM_ERRORS( ) NE 0.
      WLV_COUNT = LO_PPARSER->NUM_ERRORS( ).
      WLV_INDEX = 0.
      WHILE WLV_INDEX < WLV_COUNT.
        LO_PPARSEERROR = LO_PPARSER->GET_ERROR( INDEX = WLV_INDEX ).
        WLV_STRING = LO_PPARSEERROR->GET_REASON( ).
        TRANSLATE WLV_STRING TO UPPER CASE.
*Gets the error description updated in the Error table
        CONCATENATE TEXT-001 WC_COLON WLV_STRING INTO WLA_ERRDATA-VALUE.
        WLA_ERRDATA-TYP = WC_ERR.
        APPEND WLA_ERRDATA TO FPC_WT_ERRDATA.  CLEAR WLA_ERRDATA.
        WLV_INDEX = WLV_INDEX + 1.
      ENDWHILE.
    ENDIF.
  ENDIF.
*-- THE STREAM IS NO LONGER NEEDED, SO CLOSE IT...
  CALL METHOD LO_PISTREAM->CLOSE( ).
  CLEAR LO_PISTREAM.

*IF COUNT > 0 THEN THE XML FILE HAD TROUBLES LIKE INCOMPLETE TAGS,
*MISSING PARTS, ETC.
  IF WLV_COUNT > 0.
    EXIT.
  ENDIF.
*-- PUT THE WHOLE DOM TREE AS A LIST...
  LO_PNODE = LO_PDOCUMENT.
*  GETS THE TYPE AND VALUE OF EACH NODE OF THE DOCUMENT
  PERFORM F_FILL_TABLE USING LO_PNODE
                   CHANGING FPC_WT_XML .

  FREE : WLT_XML_C, WLT_XML_X.
ENDFORM.                    " F_READ_FILE
*&---------------------------------------------------------------------*
*&      FORM  F_FILL_TABLE
*&---------------------------------------------------------------------*
*  GETS THE TYPE AND VALUE OF EACH NODE OF THE DOCUMENT
*----------------------------------------------------------------------*
*   -->FPU_WL_PNODE     NODE
*   <--FPC_WT_XML_TABLE OUTPUT TABLE WITH ALL THE NODES IN TABULAR FORM
*----------------------------------------------------------------------*
FORM F_FILL_TABLE   USING VALUE(FPU_WL_PNODE) TYPE REF TO IF_IXML_NODE
                 CHANGING FPC_WT_XML_TABLE    TYPE TT_XML_TABLE.

* LOCAL DECLARATIONS
  DATA: WLA_XML_WA     TYPE T_XML_LIST,
        WLV_PTEXT      TYPE REF TO IF_IXML_TEXT,
        WLV_NAME       TYPE STRING,
        WLV_VALUE      TYPE STRING.

*THIS CODE CHECKS SERIALLY EACH NODE FOR ITS TYPE AND APPENDS IN THE
*COLUMN
*ELEMENT NAME OR ELEMENT VALUE ACCORDINGLY.
  CASE FPU_WL_PNODE->GET_TYPE( ).
    WHEN IF_IXML_NODE=>CO_NODE_ELEMENT.
      WLV_NAME = FPU_WL_PNODE->GET_NAME( ).

*HERE COMES THE FIELD NAME FROM THE XML FILE
      WLA_XML_WA-NAME = WLV_NAME.

      TRANSLATE WLA_XML_WA-NAME TO UPPER CASE.
*INSERT ALL FIELDS FROM XML INTO AN INTERNAL TABLE
      APPEND WLA_XML_WA TO FPC_WT_XML_TABLE.
      CLEAR WLA_XML_WA.

    WHEN IF_IXML_NODE=>CO_NODE_TEXT.
      WLV_PTEXT ?= FPU_WL_PNODE->QUERY_INTERFACE( IXML_IID_TEXT ).
      IF WLV_PTEXT->WS_ONLY( ) IS INITIAL.
*HERE COMES THE FIELD VALUE
        WLV_VALUE = FPU_WL_PNODE->GET_VALUE( ).
        WLA_XML_WA-VALUE = WLV_VALUE.
        MODIFY FPC_WT_XML_TABLE FROM WLA_XML_WA
                           INDEX SY-TABIX
                    TRANSPORTING VALUE.
        CLEAR WLA_XML_WA.
      ENDIF.
    WHEN OTHERS .
*Do nothing.
  ENDCASE.

  FPU_WL_PNODE = FPU_WL_PNODE->GET_FIRST_CHILD( ).

  WHILE NOT FPU_WL_PNODE IS INITIAL.

    PERFORM F_FILL_TABLE USING FPU_WL_PNODE
                      CHANGING FPC_WT_XML_TABLE .

    FPU_WL_PNODE = FPU_WL_PNODE->GET_NEXT( ).

  ENDWHILE.
ENDFORM.                    " F_FILL_NODE


FORM F_CREATE_FILE  USING FPU_WT_XML_TABLE TYPE TT_XML_TABLE
                 CHANGING FPC_WA_LISTE_EVT TYPE T_WA_LISTE_EVT.

  FIELD-SYMBOLS : <F_FIELDNAME>  TYPE ANY,
                  <F_VALUE>  TYPE ANY,
                  <WA>  TYPE ANY.
  DATA : WLV_FIELDNAME  TYPE CHAR15,
         WLV_FIELDVALUE TYPE CHAR20,
         WLV_COUNT      TYPE I VALUE 1,
         WLA_XML_WA     TYPE T_XML_LIST,
         WLA_PSL        TYPE T_WA_PSL ,
         WLA_EVT        TYPE T_EVT.

  DATA : BEGIN OF WLT_PSL,
          PSL TYPE TT_PSL,
         END OF WLT_PSL.

  LOOP AT FPU_WT_XML_TABLE INTO WLA_XML_WA FROM 3.
*THIS CODE WILL FILL THE WA FOR LEVEL 2 DATA OF TECH SPECS
    IF WLA_XML_WA-NAME = WC_FLG_TYP    OR
       WLA_XML_WA-NAME = WC_COD_SOC    OR
       WLA_XML_WA-NAME = WC_NUM_UAT_TC OR
       WLA_XML_WA-NAME = WC_DAT_ETD    OR
       WLA_XML_WA-NAME = WC_NUM_EVT.

      WLV_FIELDNAME = WLA_XML_WA-NAME.
      WLV_FIELDVALUE = WLA_XML_WA-VALUE.
      CONDENSE WLV_FIELDNAME.
      ASSIGN WLV_FIELDNAME TO <F_FIELDNAME>.
      ASSIGN WLV_FIELDVALUE TO <F_VALUE>.
      ASSIGN COMPONENT <F_FIELDNAME> OF STRUCTURE WLA_EVT TO <WA>.
      <WA> = <F_VALUE>.

      UNASSIGN : <WA>, <F_FIELDNAME>, <F_VALUE>.
      CLEAR : WLV_FIELDNAME, WLV_FIELDVALUE .

    ELSEIF
*THIS CODE WILL FILL THE LEVEL 5 DATA OF THE TECH SPECS
       WLA_XML_WA-NAME = WC_NUM_ORD     OR
       WLA_XML_WA-NAME = WC_COD_PSL     OR
       WLA_XML_WA-NAME = WC_LIB_PSL     OR
       WLA_XML_WA-NAME = WC_COD_PRO     OR
       WLA_XML_WA-NAME = WC_COD_STA     OR
       WLA_XML_WA-NAME = WC_DAT_PRV     OR
       WLA_XML_WA-NAME = WC_DAT_PRV_REA OR
       WLA_XML_WA-NAME = WC_DAT_EFF     OR
       WLA_XML_WA-NAME = WC_DAT_CRE     OR
       WLA_XML_WA-NAME = WC_COM1        OR
       WLA_XML_WA-NAME = WC_FLG_ALE     OR
       WLA_XML_WA-NAME = WC_COD_INCT    OR
       WLA_XML_WA-NAME = WC_COD_VIL     OR
       WLA_XML_WA-NAME = WC_COD_TRS     OR
       WLA_XML_WA-NAME = WC_COD_TRS_MLT_STP .

* ASSIGN FIELD NAME TO FIELD SYMBOL
      WLV_FIELDNAME = WLA_XML_WA-NAME.
* ASSIGN FIELD VALUE TO FIELD SYMBOL
      WLV_FIELDVALUE = WLA_XML_WA-VALUE.

      CONDENSE WLV_FIELDNAME.
*USE FIELD SYMBOLS TO FILL THE VALUES FROM
*WL_XML_WA TO STRUCTURE WA_LISTE_EVT
      ASSIGN WLV_FIELDNAME TO <F_FIELDNAME>.
      ASSIGN WLV_FIELDVALUE TO <F_VALUE>.
      ASSIGN COMPONENT <F_FIELDNAME> OF STRUCTURE WLA_PSL TO <WA>.
      <WA> = <F_VALUE>.
*APPEND LINE OF WA_PSL TO TABLE WT_PSL-PSL
      IF WLV_FIELDNAME = WC_COD_TRS_MLT_STP.
        APPEND WLA_PSL TO WLT_PSL-PSL.
        CLEAR WLA_PSL.
*COUNTER TO SURPASS THE FIRST OCCURENCE OF FIELDNAME 'EVT'
        WLV_COUNT = WLV_COUNT + 1.
      ENDIF.
      UNASSIGN : <WA>, <F_FIELDNAME>, <F_VALUE>.
      CLEAR : WLV_FIELDNAME, WLV_FIELDVALUE .
    ELSEIF
* TO APPEND ALL THE PSL ITEMS FOR SINGLE LINE OF EVT
* WLV_COUNT <> 1 MEANS OCCURENCE OF NEW DATA FOR LEVEL2 OF TS
      WLA_XML_WA-NAME = WC_EVT AND WLV_COUNT <> 1.
      APPEND LINES OF WLT_PSL-PSL TO WLA_EVT-LISTE_PSL-PSL.
      APPEND WLA_EVT TO FPC_WA_LISTE_EVT-LISTE_EVT-EVT.
      CLEAR : WLT_PSL-PSL[], WLA_EVT .
    ENDIF.
  ENDLOOP.

* TO APPEND THE LAST RECORD TO LEVEL 2 AND LEVEL 4 OF TS
  APPEND LINES OF WLT_PSL-PSL TO WLA_EVT-LISTE_PSL-PSL.
  APPEND WLA_EVT TO FPC_WA_LISTE_EVT-LISTE_EVT-EVT.
  CLEAR WLA_EVT .

  FREE : WLT_PSL-PSL[].

ENDFORM.                    " F_CREATE_FILE

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.