Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Requirement: An XML file contains header and line item details. We need to parse the XML and finally move the data to the internal table in ABAP.

Step 1: We need to analyse the XML file and create the structures accordingly using SE11.

Our XML looks as below.




Step 2: Create Structures in SE11 as per the above hierarchy.

Header Structure:



 

Line Item Structure



 

Create a table type for the Line Item structure



 

Final Structure:

Create a final structure with the header structure and line Item table types as shown below.



Step 3: Go to XSLT_TOOL transaction and create the transformation as shown below.



 

Step 4: A popup appears, enter the description and give the transformation type as Simple Transformation.



 

Step 5:  Click on the magic wizard icon.



Step 6: Right click on the ROOT and Insert new root. Enter the Root-Name and Type Name as Final structure name we have created in SE11. I.e zpipenomincreate.



Step 7: Below hierarchy will be created automatically.


Step 8: Drag the root node to the Simple transformation on the right-hand side.

 

Step 9: Save, go back and click on the source code tab.

All the bindings will be automatically created.



 

Step 10: All the Tags will be created in Upper case by default. As our original XML file tags contain both upper and lowercase tags we need to modify the above code which should be similar to theOur original  XML File .
* Note : XML File is attached to this blog.


For Example our XSLT tag looks in upper case compared to original XML

XLST TAG : <PIPELINENOMINATIONCREATE>

XML Tag: <PipelineNominationCreate>

we need to modify the XLST tags similar to original xml tags.


<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
<tt:root name="ROOT" type="?"/>
<tt:root type="ddic:ZPIPENOMINCREATE" name="PIPELINENOMINATIONCREATE"/>
<tt:template>
<PipelineNominationCreate>
<PipelineNominationCreateHeader>
<PipelineCycle tt:value-ref="PIPELINENOMINATIONCREATE.PIPELINENOMINATIONCREATEHEADER.PIPELINECYCLE"/>
<PipelineCycleYear tt:value-ref=".PIPELINENOMINATIONCREATE.PIPELINENOMINATIONCREATEHEADER.PIPELINECYCLEYEAR"/>
<PipelineSequence tt:value-ref=".PIPELINENOMINATIONCREATE.PIPELINENOMINATIONCREATEHEADER.PIPELINESEQUENCE"/>
<ReferenceNumber tt:value-ref=".PIPELINENOMINATIONCREATE.PIPELINENOMINATIONCREATEHEADER.REFERENCENUMBER"/>
<Description tt:value-ref=".PIPELINENOMINATIONCREATE.PIPELINENOMINATIONCREATEHEADER.DESCRIPTION"/>
</PipelineNominationCreateHeader>
<PipelineNominationCreateDetails>
<tt:loop ref=".PIPELINENOMINATIONCREATE.PIPELINENOMINATIONCREATEDETAIL">
<PipelineNominationCreateLineItem>
<LineItemNumber tt:value-ref="LINEITEMNUMBER"/>
<Quantity tt:value-ref="QUANTITY"/>
<UnitOfMeasureCode tt:value-ref="UNITOFMEASURECODE"/>
<ConfirmationDateTime tt:value-ref="CONFIRMATIONDATETIME"/>
<ConfirmationStatusCode tt:value-ref="CONFIRMATIONSTATUSCODE"/>
</PipelineNominationCreateLineItem>
</tt:loop>
</PipelineNominationCreateDetails>
</PipelineNominationCreate>
</tt:template>
</tt:transform>

 

Step 11: Save and activate the transformation.

Step 12: We need to create a report to get the XML data into the internal table.

REPORT z_xml_to_itab.

TYPE-POOLS abap.

*Input file with path as constant
CONSTANTS gs_file TYPE string VALUE 'C:\Users\girishv\Desktop\test.xml'.

* Table for storing the XML content from file
DATA: gt_itab         TYPE STANDARD TABLE OF char2048,
gs_create_nomin TYPE           zpipenomincreate,
gt_create_nomin TYPE  TABLE OF zpipenomincreate,
lv_msg         TYPE string,
lv_header      TYPE string,
lv_item      TYPE string,
lt_lines TYPE ZPIPENOMINCREATELINES_T,
ls_lines TYPE ZPIPENOMINCREATELINES,
r_excep         TYPE REF TO cx_st_match_element .

* Get the XML file from your client
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename                = gs_file
CHANGING
data_tab                = gt_itab
EXCEPTIONS
file_open_error         = 1
file_read_error         = 2
no_batch                = 3
gui_refuse_filetransfer = 4
invalid_type            = 5
no_authority            = 6
unknown_error           = 7
bad_data_format         = 8
header_not_allowed      = 9
separator_not_allowed   = 10
header_too_long         = 11
unknown_dp_error        = 12
access_denied           = 13
dp_out_of_memory        = 14
disk_full               = 15
dp_timeout              = 16
not_supported_by_gui    = 17
error_no_gui            = 18
OTHERS                  = 19.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

* Perform the XSLT stylesheet
TRY.
*--- Data in the excel will be collected in gs_create_main.
CALL TRANSFORMATION ZXML_TRANSFORM
SOURCE XML gt_itab
RESULT pipelinenominationcreate = gs_create_nomin.
APPEND gs_create_nomin to gt_create_nomin.
CLEAR gs_create_nomin.
CATCH cx_st_match_element INTO r_excep.
lv_msg = r_excep->get_text( ).
WRITE lv_msg.
ENDTRY.

LOOP AT gt_create_nomin INTO gs_create_nomin.
* Header
WRITE : 'Header'. NEW-LINE.
CONCATENATE gs_create_nomin-pipelinenominationcreateheader-PIPELINECYCLE
gs_create_nomin-pipelinenominationcreateheader-PIPELINECYCLEYEAR
gs_create_nomin-pipelinenominationcreateheader-PIPELINESEQUENCE
gs_create_nomin-pipelinenominationcreateheader-REFERENCENUMBER
gs_create_nomin-pipelinenominationcreateheader-DESCRIPTION INTO lv_header SEPARATED BY space.
WRITE : / lv_header.
NEW-LINE.

*LOOP AT
lt_lines = gs_create_nomin-pipelinenominationcreatedetail.

WRITE 'Line Items'.
NEW-LINE.
LOOP AT lt_lines INTO ls_lines.
CONCATENATE
ls_lines-LINEITEMNUMBER
ls_lines-QUANTITY
ls_lines-UNITOFMEASURECODE
ls_lines-CONFIRMATIONDATETIME
ls_lines-CONFIRMATIONSTATUSCODE INTO lv_item SEPARATED BY space..

WRITE / lv_item.


Step 13: Execute the Report.

5 Comments
Labels in this area