Converting an XML file with many hierarchy levels to ABAP format

...Previous

In this part of the code  

Z_XML_IDOC should be created in XSLT Editor and write code for that as follows:-

We can go there by double clicking on Z_XML_IDOC.Create it and after copying this code activate it.  

XSLT is a language is used to convert XML from one format to another format.  

Copy paste the following code in XSLT editor :-  

Some rules which were applied for conversion to XSLT are:-

IDOC_GET is used to get the data from IDOC.In the XML file the main node is IDOC which has E1MARAM and EDIDC_40 which in turn contains MSGFN,MATNR and E1MAKTM and EDIDC_40 contains MSGFN and DOCNUM respectively.  

The same type of structure which is used in our program needs to be filled.In our program EDIDC_40 is used as ZEDIDC40 and E1MARAM is used as ZE1MARAM_TEST1.  

The basic template is already filled when we enter the XSLT editor.

We use IDOC_GET to use the IDOC data.We should use the tags to be filled in the hierarchy structure as we declared it in the program.To use the values we need to use the structures in the XML file.Use the corresponding path to access the values.  

After transformation the gt_zeu_matmas03 is filled which can be used for displaying.  

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <IDOC_GET>
          <xsl:apply-templates select="//IDOC"/>
        </IDOC_GET>
      </asx:values>
    </asx:abap>
  </xsl:template>
  <xsl:template match="IDOC">
    <item>
      <IDOC>
        <ZEDIDC40>
         <MESTYP><xsl:value-of select="EDI_DC40/MESTYP"/></MESTYP>
         <DOCNUM><xsl:value-of select="EDI_DC40/DOCNUM"/></DOCNUM>
        </ZEDIDC40>
        <ZE1MARAM_TEST1>
          <MSGFN><xsl:value-of select="E1MARAM/MSGFN"/></MSGFN>
          <MATNR><xsl:value-of select="E1MARAM/MATNR"/></MATNR>
         <E1MAKTM>
           <MSGFN><xsl:value-of select="E1MARAM/E1MAKTM/MSGFN"/></MSGFN>
           <SPRAS><xsl:value-of select="E1MARAM/E1MAKTM/SPRAS"/></SPRAS>
           <MAKTX><xsl:value-of select="E1MARAM/E1MAKTM/MAKTX"/></MAKTX>
           <SPRAS_ISO><xsl:value-of select="E1MARAM/E1MAKTM/SPRAS_ISO"/></SPRAS_ISO>
         </E1MAKTM>
        </ZE1MARAM_TEST1>
      </IDOC>
    </item>
  </xsl:template>
</xsl:transform> 
  

*Come back to ABAP editor and copy this code for error handling.

  CATCH cx_root INTO gs_rif_ex.

    gs_var_text = gs_rif_ex->get_text( ).
    
MESSAGE gs_var_text TYPE 'E'.

ENDTRY.  

*Display the file contents on screen.
IF
 sy-subrc = 0.
  
LOOP at gt_zeu_matmas03 INTO gs_zeu_matmas03.
    
WRITE:  gs_zeu_matmas03-idoc-zedidc40-docnum, “document no
          / gs_zeu_matmas03-idoc-zedidc40-mestyp, “message type
          / gs_zeu_matmas03-idoc-ze1maram_test1-msgfn,”message function
          / gs_zeu_matmas03-idoc-ze1maram_test1-matnr,”material no

*all fields of E1MAKTM segment
          / gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-msgfn,
          / gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-spras,
          / gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-maktx,
          / gs_zeu_matmas03-idoc-ze1maram_test1-e1maktm-spras_iso.
  
ENDLOOP.
ENDIF.  

We can see the values filled in the heirarchy structure of IDOC as follows:-

 

Execute the ABAP program and we get the file contents displayed as below.So we have converted the given XML File to a structure and display the contents.

Final output of the program is:-

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image